【发布时间】:2020-10-14 13:08:13
【问题描述】:
在我的项目中,我们为 E2E 测试需要针对的大多数元素提供了类:
<div class="just-an-item" [e2e] [e2e-class]="button-to-trigger-something">...</div>
编译成什么
<div class="just-an-item e2e-button-to-trigger-something">...</div>
感谢这个指令
@Directive({
selector: '[e2e]'
})
export class E2eClassDirective implements OnInit {
@HostBinding('class')
public classes: string;
@Input('e2e-class')
public readonly className: string;
constructor (
private host: ElementRef,
private renderer: Renderer2
) {
}
public ngOnInit (): void {
this.renderer.addClass(
this.host.nativeElement,'e2e-' + this.className // <-------
);
}
}
当然我有一个模块
@NgModule({
...
declarations: [ ... ... E2eClassDirective ],
...
})
export class MainModule {}
我想在编译时禁用此功能,而不是在执行时。
换句话说,我不希望这样:
public ngOnInit (): void {
if( !environment.production ) {
this.renderer.addClass(
this.host.nativeElement,'e2e-' + this.className // <-------
);
}
}
因为这仍然意味着文本将在编译版本中,并且将执行逻辑。相反,如果我们在生产模式下构建应用程序,我希望能够“禁用”它,这样它就不会进入/dist。
这可能吗?
【问题讨论】:
-
您是否尝试过有条件地将此指令添加到您的模块中?
标签: angular angular-directive angular9