我有一个UserService其中包含如下一些静态方法:如下所示无法注入服务angular2-RC4
import {Injectable} from '@angular/core';
@Injectable()
export class UserInfoService {
private static _userInfo: any;
static put(userInfo:any) {
this._userInfo = userInfo;
}
static getRole() : string {
if (this._userInfo) {
return this._userInfo.role;
}
return '';
}
static getUserInfo() : string {
return this._userInfo;
}
}
我注入它在boot.ts:
bootstrap(AppComponent, [ UserInfoService])
我在另一个组件使用此服务如下:
import {Component} from '@angular/core';
import {UserInfoService} from "../service/user.info";
@Component({
selector: 'nav-bar',
templateUrl: 'template/navbar.html'
});
export class NavigationComponent {
constructor() {
this.userInfo = UserInfoService.getUserInfo();
}
}
这NavigationComponent调用无法访问UserInfoService。 注意:这与angular2-rc1一起工作。升级到rc4后,我遇到了这个问题。 请帮忙。
你必须注入''里面constructor' UserInfoService'得到它的实例.. –