问题描述: 当使用Angular属性指令时,出现了错误提示“期望1个参数但得到了0个”。
解决方法: 这个错误通常发生在我们使用属性指令时没有传递必要的参数。根据错误提示,我们需要检查我们的代码并确保传递了正确的参数。
下面是一个示例代码,展示了如何解决这个问题:
highlight
的属性指令。import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[highlight]'
})
export class HighlightDirective {
@Input() highlightColor: string;
constructor(private el: ElementRef) { }
ngOnInit() {
this.highlight();
}
private highlight() {
if (this.highlightColor) {
this.el.nativeElement.style.backgroundColor = this.highlightColor;
}
}
}
This text will be highlighted in yellow.
在上面的示例中,我们将 确保在使用属性指令时传递了正确的参数,这样就能解决“期望1个参数但得到了0个”的错误。highlight
属性指令应用于一个[highlightColor]
绑定传递了一个值为'yellow'
的参数。
相关内容