Dependency injection 角度6+;提供地址:';根';引发静态注入错误
我已经在谷歌上搜索了很多次,但似乎找不到其他人有这个问题,所以我肯定错过了什么。我正在将所有AppModule服务转换为使用providedIn:'root'方法,但它似乎不起作用Dependency injection 角度6+;提供地址:';根';引发静态注入错误,dependency-injection,angular6,Dependency Injection,Angular6,我已经在谷歌上搜索了很多次,但似乎找不到其他人有这个问题,所以我肯定错过了什么。我正在将所有AppModule服务转换为使用providedIn:'root'方法,但它似乎不起作用 import { Injectable } from '@angular/core'; Injectable({ providedIn: 'root' }) export class CommonService{ UserName : string = 'Guest'; Roles : Ar
import { Injectable } from '@angular/core';
Injectable({
providedIn: 'root'
})
export class CommonService{
UserName : string = 'Guest';
Roles : Array<any> = [];
Theme: string = 'standard';
constructor(){}
}
在运行时,这是控制台中的错误:
StaticInjectorError(AppModule)[NavBar->CommonService]:
StaticInjectorError(平台:核心)[NavBar->CommonService]:
NullInjectorError:没有CommonService的提供程序强>
我已经检查了文档,我看不出哪里出了问题。我错过了什么
忘了提及,NavBar是SharedModule中声明的组件,SharedModule在AppModule中导入。您可能缺少Injectable上的前导“@”:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CommonService{
UserName : string = 'Guest';
Roles : Array<any> = [];
Theme: string = 'standard';
constructor(){}
}
从'@angular/core'导入{Injectable};
@注射的({
providedIn:'根'
})
导出类公共服务{
用户名:string='Guest';
角色:数组=[];
主题:字符串='standard';
构造函数(){}
}
IDE中可能没有错误,但这可能就是问题所在
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CommonService{
UserName : string = 'Guest';
Roles : Array<any> = [];
Theme: string = 'standard';
constructor(){}
}