的所有參數我完全是Angular的新手,並試圖從Angular嚮導中注入basic structural directive。這裏是我的指令:Angular 4指令錯誤:無法解析指令
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 { }
在我TradeComponent
的模板中使用HTML的以下部分:
...
<p *pcDropdown="true">
TEST
</p>
...
但我得到的錯誤:
Uncaught Error: Can't resolve all parameters for DropdownDirective: ([object Object], [object Object], ?).
Webstorm也基本我@Directive
裝飾,說以下內容:
Angular: Can't resolve all parameters for DropdownDirective in /home/commercialsuicide/Desktop/my-app/src/client/app/dropdown.directive/dropdown.directive.ts: ([object Object], [object Object], ?)
它還說,我pcDropdown
輸入未使用:
需要說的,那我已經看到this answer和emitDecoratorMetadata
已經設置爲true
tsconfig.json
。
請點,我拼寫錯誤或忘記包括在我的代碼中的東西。
非常感謝
嗯,這個解決方案是非常基礎的。非常感謝你的幫助! –
提示:錯誤信息用'?'指出哪個參數導致錯誤'DropdownDirective:([object Object],[object Object],?)。'。很高興聽到這個解決它已經爲你:) –