【发布时间】:2016-04-06 08:11:57
【问题描述】:
我昨天已经完成了所有工作。而今天,在我重新启动环境后,我尝试注入的服务之一现在始终是null。
这是我的顶级组件(app.component.ts):
@Component({
selector: 'priz-app',
moduleId: module.id,
templateUrl: './app.component.html',
directives: [ROUTER_DIRECTIVES, SecureRouterOutlet],
providers: [ROUTER_PROVIDERS, AuthenticationService]
})
该组件的模板包含:
<secure-outlet signin="Login" unauthorized="AccessDenied"></secure-outlet>
其中secure-outlet的实现如下:
@Directive({
selector: 'secure-outlet'
})
export class SecureRouterOutlet extends RouterOutlet {
@Input()
signin: string;
@Input()
unauthorized:string;
@Input()
nameAttr: string;
constructor(_elementRef: ElementRef, _loader: DynamicComponentLoader,
private parentRouter: Router,
private authService: AuthenticationService,
public injector: Injector) {
super(_elementRef, _loader, parentRouter, null);
}
...
在构造函数中,以及指令中的其他任何地方,authService 始终是null。我尝试在指令内、主要组件中、甚至在引导程序中使用 AuthenticationService 定义提供程序,但没有任何效果。
我错过了什么?
谢谢,
【问题讨论】:
-
首先从 providers 数组中移除
ROUTER_PROVIDERS并在引导函数中使用它。
标签: dependency-injection angular angular-directive