【发布时间】:2016-06-11 21:44:17
【问题描述】:
我在将单例服务注入指令时遇到问题。 我有服务:
@Injectable()
export class AuthService{ ... }
我把它放到引导程序中。
bootstrap(AppComponent, [AuthService, ...]);
我制定了指令,保护我的组件:
@Directive({
selector: '[protected]'
})
export class ProtectedDirective {
constructor(private authService:AuthService) { ... }
}
...并添加到组件之一
@Component({
selector: 'dashboard',
directives: [ProtectedDirective],
template: '<div protected></div',
})
export class DashboardCmp { }
在控制台中我看到一个错误:
ORIGINAL EXCEPTION: No provider for AuthService!
如果我将提供程序添加到 DashboardCmp,一切正常,但它不是单例服务。我在其他组件中设置了它的属性,当我在指令中时我看不到它们。
【问题讨论】:
标签: dependency-injection angular