Dependency injection 服务类不在Angular2中注入
昨天我已经把一切都安排好了。今天,在我重新启动环境之后,我尝试注入的服务之一现在总是Dependency injection 服务类不在Angular2中注入,dependency-injection,angular,angular-directive,Dependency Injection,Angular,Angular Directive,昨天我已经把一切都安排好了。今天,在我重新启动环境之后,我尝试注入的服务之一现在总是null 这是我的顶级组件(app.component.ts): 该组件的模板包含: 其中,安全出口的实施如下: @Directive({ selector: 'secure-outlet' }) export class SecureRouterOutlet extends RouterOutlet { @Input() signin: string; @Input()
null
这是我的顶级组件(app.component.ts):
该组件的模板包含:
其中,安全出口
的实施如下:
@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
定义提供者,但没有任何效果
我错过了什么
谢谢,不要将
提供者:[ROUTER\u providers]
添加到组件,只将添加到引导()
尝试提供与原始RouterOutlet
类相同的参数,并在其后面添加您自己的依赖项。
虽然不确定这是否有帮助,但我认为不久前有人提到有一个奇怪的问题:
constructor(
_elementRef: ElementRef,
_loader: DynamicComponentLoader,
private parentRouter: Router,
@Attribute('name') nameAttr: string, // <= added
private authService: AuthenticationService,
public injector: Injector) {
构造函数(
_elementRef:elementRef,
_加载程序:DynamicComponentLoader,
专用路由器:路由器,
@属性('名称')nameatr:string,//首先从提供者数组中删除路由器\u提供者
,并在引导函数中使用它。好的,这实际上是可行的,甚至有点道理。但是,现在我发现了这个错误:/Users/ShurikAg/Dev/angular2/ng2/src/shared/directions/securerouter.directive.ts行47 col 17在类的构造函数中“SufRealOutExchange”,参数“NAMEADATR”使用@属性装饰器,它被认为是一种错误的做法。请考虑构造“@输入())名称:"
.Hmm,这对我来说毫无意义。我认为构造函数中依赖项的数量、种类和顺序应该无关紧要。路由器出口的实例化似乎有些可疑。我在讨论中看到了这一点。我应该如何处理错误?我的意思是,它仍然有效,但@attribute有一个原因e decorator是一个糟糕的做法。啊,没有阅读完整的注释。这是一个非常奇怪的消息。不知道是什么产生了它。我也不知道它有什么不好。如果你只对静态值感兴趣,不想被通知以后的更新@Attribute
,就可以了。@Input()
在实际用例中会非常麻烦。我在Angular2 GitHub repo中找不到此错误消息。
constructor(
_elementRef: ElementRef,
_loader: DynamicComponentLoader,
private parentRouter: Router,
@Attribute('name') nameAttr: string, // <= added
private authService: AuthenticationService,
public injector: Injector) {