更新时间:2021-10-06 05:42:48
这是一个已知问题,因为 Window 是一个打字稿接口.你需要通过创建一个假类来欺骗编译器,比如 WindowWrapper.ts
It is known issue because Window is an typescript interface.
You need to trick a compiler by creating a fake class say WindowWrapper.ts
@Injectable()
export class WindowWrapper extends Window { }
export function getWindow() { return window; }
app.module:
app.module:
import { WindowWrapper, getWindow } from './WindowWrapper';
providers: [
{provide: WindowWrapper, useFactory: getWindow}
],
指令:
import { Directive, ElementRef, HostListener, Input, Inject } from '@angular/core';
import { WindowWrapper } from './WindowWrapper';
@Directive({
selector: '[newTab]'
})
export class OpenLinkInNewTabDirective {
constructor(
private el: ElementRef,
@Inject(WindowWrapper) private win: Window) {}
@Input('routerLink') link: string;
@HostListener('mousedown') onMouseEnter() {
this.win.open(this.link || 'main/default');
}
}
check more details on that isse and that particular comment