简体   繁体   English

如何以新的Angular CLI格式初始化模块加载时的服务

[英]How to initilize service on module load in new Angular cli format

I am iniliziing service in app.module 我在app.module中初始化服务

  providers: [
    RoleChildTreeService
    ],
  bootstrap: [AppComponent,RoleChildTreeService]

but i got error 但我有错误

directive_resolver.js:39 Uncaught Error: No Directive annotation found on RoleChildTreeService directive_resolver.js:39未捕获的错误:在RoleChildTreeService上找不到指令注释

Why you want to inject in bootstrap:[...] ? 为什么要注入bootstrap:[...] If you want a service which should available to your whole application, then add 如果您希望整个应用程序都可以使用该服务 ,请添加

import { RoleChildTreeService } from './yourPath/roleChildTree.service';

in your app.component.ts and then 在您的app.component.ts ,然后

providers: [ RoleChildTreeService, ... ]

Which you can use anywhere. 您可以在任何地方使用。 Please correct me if you want to do something else. 如果您想做其他事情,请纠正我。

Just inject the service to the module constructor to get it initialized 只需将服务注入模块构造函数即可对其进行初始化

@NgModule({
  providers: [
    RoleChildTreeService
    ],
  bootstrap: [AppComponent]
})
export class SomeModule {
  constructor(roleChildService:RoleChildTreeService) {}
}

It's a while I saw this but AFAIR it should work. 我已经看到了一段时间,但是应该可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM