我对 Angular 完全陌生并尝试注入基本结构指令 https://angular.io/guide/structural-directives#the-myunless-property来自角度指南。这是我的指令:
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[pcDropdown]'
})
export class DropdownDirective {
private hasView = false;
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private items
) { }
@Input() set pcDropdown(condition: boolean) {
if (!condition && !this.hasView) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.hasView = true;
} else if (condition && this.hasView) {
this.viewContainer.clear();
this.hasView = false;
}
}
}
我正在尝试将它注入我的TradeModule
:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
import { TradeComponent } from './trade.component';
import { DropdownDirective } from '../dropdown.directive/dropdown.directive';
@NgModule({
imports: [
CommonModule,
SharedModule
],
declarations: [TradeComponent, DropdownDirective],
exports: [DropdownDirective]
})
export class TradeModule { }
并在我的中使用以下 HTML 部分TradeComponent
的模板:
...
<p *pcDropdown="true">
TEST
</p>
...
但我收到错误:
未捕获的错误:无法解析 DropdownDirective 的所有参数:
([对象对象],[对象对象],?)。
Webstorm 也是我的基础@Directive
装饰者并说出以下内容:
Angular:无法解析 DropdownDirective 的所有参数
/home/commercialsuicide/Desktop/my-app/src/client/app/dropdown.directive/dropdown.directive.ts:
([对象对象], [对象对象], ?)
它还说我的pcDropdown
输入未使用:
需要说的是,我已经看到了这个答案 https://stackoverflow.com/a/42717565/6053654 and emitDecoratorMetadata
已设置为true
in tsconfig.json
.
请指出我拼写错误或忘记在代码中包含某些内容的地方。
非常感谢