Angular 6 service injection exception

风格不统一 提交于 2019-12-06 16:46:05

you need to add the service to the providers in the module of course remember to import the service

    @NgModule({
      declarations: [
        AppComponent,
        HeaderComponent,
        SerachBarComponent,
        SearchReasultComponent,
        ResultItemComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        HttpClientModule,
        FormsModule
      ],
      providers: [
        ReactiveTwitterService 
      ],
      bootstrap: [AppComponent]
    })

in your code constructor(private _twitterService: ReactiveTwitterService) { } there is no way to initialize the private tweetTag: string therefore it still fail, and the @Injectable({ providedIn: 'root' }) does not act the same as providers: [ReactiveTwitterService]

Your service should be made available to your component or the module as a provider. You can add it to providers: array at appmodule or to the individual module and inject it in component for use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!