且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法解析自定义指令的所有参数

更新时间:2022-06-27 05:53:35

这是已知问题,因为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');
    }
}

查看有关该 isse 以及该特定

check more details on that isse and that particular comment