在HeaderComponent
上,我正在使用authorize
指令:
<li *authorize="'SignedIn'"><button (click)="signIn()">Sign In</button></li>
但是我得到了错误:
Can't bind to 'authorize' since it isn't a known property of 'li'.
HeaderComponent
被添加到HeaderModule:
@NgModule({
declarations: [
HeaderComponent
],
imports: [
CommonModule,
PopupModule
],
exports: [
HeaderComponent
]
})
将HeaderModule
和AuthorizeDirective
添加到SharedModule
中:
@NgModule({
declarations: [
AuthorizeDirective
],
imports: [
CommonModule
],
exports: [
HeaderModule,
AuthorizeDirective
]
})
并将SharedModule
添加到AppModule
。
我想念什么?
authorize指令是:
@Directive({
selector: '[authorize]'
})
export class AuthorizeDirective implements OnInit {
private notifier: Subscription;
requirement: Requirement;
id: number;
@Input() set authorize(requirement: string) {
this.requirement = Requirement[requirement];
}
@Input() set authorizeId(id: number) {
this.id = id;
}
ngOnInit() {
}
}
答案 0 :(得分:2)
由于HeaderModule
是SharedModule
的子代,因此您需要重新考虑一些设计。您无法将指令从SharedModule
(父级)导入HeaderModule
(子级)。您可以将HeaderModule
做成独立于SharedModule
的模块,也可以创建其他保存AuthorizeDirective
的通用模块。
答案 1 :(得分:0)
我认为授权是Input
,因此您可以像这样使用授权
<li [authorize]="'SignedIn'"><button (click)="signIn()">Sign In</button></li>