Angular 4 指令错误:无法解析指令的所有参数 [英] Angular 4 directive error: Can't resolve all parameters for directive

查看:27
本文介绍了Angular 4 指令错误:无法解析指令的所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Angular 完全陌生,正在尝试注入

它还说我的 pcDropdown 输入未使用:

需要说的是,我已经看到了这个答案 并且 emitDecoratorMetadata 已经设置为truetsconfig.json 中.

请指出我拼写错误或忘记在代码中包含某些内容的地方.

非常感谢

解决方案

private items 缺少类型参数.如果 Angular 无法将所有参数解析给提供者,则它无法创建组件实例.
解析为提供程序仅适用于参数类型和 @Inject(...) 注释.

如果您不想注入 items,请删除该参数.不存在您需要自己创建组件实例来显式传递参数的情况.

I'm totally new to Angular and trying to inject basic structural directive from Angular guide. Here is my directive:

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;
    }
  }
}

I'm trying to inject it in my 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 { }

And use the following part of HTML in my TradeComponent's template:

...
<p *pcDropdown="true">
  TEST
</p>
...

But I'm getting the error:

Uncaught Error: Can't resolve all parameters for DropdownDirective: ([object Object], [object Object], ?).

Webstorm is also underlying my @Directive decorator and say the following:

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], ?)

It also say that my pcDropdown input is unused:

Need to say, that i already saw this answer and emitDecoratorMetadata is already set to true in tsconfig.json.

Please, point where I misspelled or forgot to include something in my code.

Many thanks

解决方案

private items is missing a type parameter. Angular can't create component instances if it can't resolve all parameters to providers.
Resolving to providers only works with parameter types and @Inject(...) annotations.

If you don't want items to be injected, remove the parameter. There is no situation where you would need to create a component instance yourself to pass the parameter explicitely.

这篇关于Angular 4 指令错误:无法解析指令的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆