无法解析自定义指令的所有参数 [英] Can't resolve all parameters for custom directive
问题描述
我创建了一个自定义指令以在新标签中打开链接,当在ng serve中运行时,它可以工作.但是,当我尝试ng build --prod时,它显示了此错误:
I created a custom directive to open a link in a new tab, when running in ng serve, it works. However, when I tried in ng build --prod, it showed this error:
中的错误无法解析C:/Users/myApp/src/app/directives/open-link-in-new-tab.directive.ts中的OpenLinkInNewTabDirective的所有参数:([object Object] ,?).
ERROR in Can't resolve all parameters for OpenLinkInNewTabDirective in C:/Users/myApp/src/app/directives/open-link-in-new-tab.directive.ts: ([object Object], ?).
这是指令:
import { Directive, ElementRef, HostListener, Input, Inject } from '@angular/core';
@Directive({ selector: '[newTab]' })
export class OpenLinkInNewTabDirective {
constructor(
private el: ElementRef,
@Inject(Window) private win:Window
) {}
@Input('routerLink') link: string;
@HostListener('mousedown') onMouseEnter() {
this.win.open(this.link || 'main/default');
}
}
我已经在tsconfig.json中设置了"emitDecoratorMetadata":true. 先感谢您.
I set "emitDecoratorMetadata": true in tsconfig.json already. Thank you in advance.
推荐答案
这是已知问题,因为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
这篇关于无法解析自定义指令的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!