Angular no provider for NameService

后端 未结 18 1761
遇见更好的自我
遇见更好的自我 2020-11-30 18:44

I\'ve got a problem loading a class into an Angular component. I\'ve been trying to solve it for a long time; I\'ve even tried joining it all in a single file. What I have i

18条回答
  •  有刺的猬
    2020-11-30 19:26

    You should be injecting NameService inside providers array of your AppModule's NgModule metadata.

    @NgModule({
       imports: [BrowserModule, ...],
       declarations: [...],
       bootstrap: [AppComponent],
       //inject providers below if you want single instance to be share amongst app
       providers: [MyService]
    })
    export class AppModule {
    
    }
    

    If you want to create an Dependency for particular component level without bothering about state of your application, then you can inject that dependency on component providers metadata option like accepted @Klass answer shown.

提交回复
热议问题