我有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
无效?
答案 0 :(得分:0)
我不确定,但是可能的原因是HelperService
也被AuthService
使用,而UserManagementService
是通过Angular
添加的。我认为UserManagementService
试图注入HelperService
并发现无法添加HelperService
。为了解决此问题,我在模块级别注入了providers
,以便它对所有组件都可用。
我想知道在组件级别颠倒providers:[UserManagementService, HelperService]
中的服务顺序是否可行,但是没有实现,即providers:[HelperService,UserManagementService]
和RandomForestClassifier
都不起作用