简体   繁体   English

为什么我的服务不能正确注入到 Angular 的组件中?

[英]Why won't my Service inject properly into a component in Angular?

The structure of my Angular App (and relevant portions) is as follows:我的 Angular 应用程序(和相关部分)的结构如下:

app/app.module.ts应用程序/app.module.ts

 import { DbService } from './db.service' const APP_CONTAINERS = [ DefaultLayoutComponent, DefaultHeaderComponent, ]; @NgModule({ declarations: [ ...APP_CONTAINERS ], providers: [ DbService ] }) export class AppModule {}

app/containers/default-layout.component.ts app/containers/default-layout.component.ts

 import { Component, HostBinding, Inject } from '@angular/core'; import { DbService } from '../../db.service'; import { navItems } from './_nav'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) @Component({ selector: 'app-dashboard', templateUrl: './default-layout.component.html' }) export class DefaultLayoutComponent { @HostBinding('class.c-app') cAppClass = true; constructor(private DbService: DbService) {} async ngOnInit() { console.log('working') } }

I use this same service successfully in other portions of my site but they all have specific module.ts files.我在网站的其他部分成功使用了相同的服务,但它们都有特定的 module.ts 文件。 I followed the Angular tutorial to get to where I am but I am still getting errors no matter how I try to inject this service.我按照 Angular 教程到达我所在的位置,但无论我如何尝试注入此服务,我仍然遇到错误。

I also tried using @Inject in my constructor but received the same error.我也尝试在我的构造函数中使用 @Inject 但收到相同的错误。

I currently receive the following error in my console:我目前在控制台中收到以下错误:

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(e)[e -> e -> e -> e]: 
  NullInjectorError: No provider for e!
NullInjectorError: R3InjectorError(e)[e -> e -> e -> e]: 
  NullInjectorError: No provider for e!

I use this same service successfully in other portions of my site but they all have specific module.ts files我在网站的其他部分成功使用了相同的服务,但它们都有特定的 module.ts 文件

So it is unclear how you divide your modules.所以目前还不清楚你是如何划分模块的。 It doesn't look from the module file like that component is in the module that provides the service.从模块文件来看,它不像该组件在提供服务的模块中那样。 The service has to be in the providers for the injection tree where the component resides.该服务必须位于组件所在的注入树的提供者中。 If that module file is for a different module, then the injector has to way to know what you want provided for the service.如果该模块文件用于不同的模块,则注入器必须知道您希望为服务提供什么。

One solution would be to add it in the providers for your component.一种解决方案是将它添加到您的组件的提供程序中。 That would get a different instance of the service than in your other modules however:但是,这将获得与其他模块不同的服务实例:

@Component({
  selector: 'app-dashboard',
  templateUrl: './default-layout.component.html',
  providers: [DbService]
})
export class DefaultLayoutComponent {

You could also add it to the providers for the module your component resides in, but that would get a different instance as well.您也可以将它添加到您的组件所在模块的提供程序中,但这也会获得不同的实例。

I think the best way would be to define the service so that it should be provided in the root module.我认为最好的方法是定义服务,以便它应该在根模块中提供。 This way you don't have to add it in the providers anywhere, angular will automatically add it to the root module's providers when it is required.这样你就不必在任何地方的提供者中添加它,angular 会在需要时自动将它添加到根模块的提供者中。 That leads to what Octavian was saying in his comment.这导致了屋大维在他的评论中所说的话。 I think you just put this code incorrectly in the component instead of the service.我认为您只是将此代码错误地放在组件而不是服务中。 Move this to the service and remove the service from the providers arrays in your module(s):将此移动到服务并从模块中的providers数组中删除服务:

@Injectable({
  providedIn: 'root'
})

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

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