在组件中的提供程序中添加服务时,收到没有提供程序错误

时间:2018-09-22 15:16:15

标签: angular6

我有src/main/resources/static使用的服务HelperService

NavComponentComponent

nav-component.component.ts

@Injectable()
export class HelperService {
...
}

我正在@Component({ selector: 'app-nav-component', templateUrl: './nav-component.component.html', styleUrls: ['./nav-component.component.css'], providers:[UserManagementService, HelperService] }) export class NavComponentComponent implements OnInit { constructor(private userManagementService: UserManagementService, private fb:FormBuilder, private helper:HelperService) { } .. } 中使用NavComponent

AppComponent

但是我遇到了错误

<app-nav-component></app-nav-component>

但是当我向服务提供模块级别时,我的程序就可以了!为什么仅在ERROR Error: StaticInjectorError(AppModule)[NavComponentComponent -> HelperService]: StaticInjectorError(Platform: core)[NavComponentComponent -> HelperService]: NullInjectorError: No provider for HelperService! at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:1062) at resolveToken (core.js:1300) at tryResolveToken (core.js:1244) at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1141) at resolveToken (core.js:1300) at tryResolveToken (core.js:1244) at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1141) 中使用providers:HelperService无效?

1 个答案:

答案 0 :(得分:0)

我不确定,但是可能的原因是HelperService也被AuthService使用,而UserManagementService是通过Angular添加的。我认为UserManagementService试图注入HelperService并发现无法添加HelperService。为了解决此问题,我在模块级别注入了providers,以便它对所有组件都可用。

我想知道在组件级别颠倒providers:[UserManagementService, HelperService]中的服务顺序是否可行,但是没有实现,即providers:[HelperService,UserManagementService]RandomForestClassifier都不起作用