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

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

问题描述

我对Angular完全陌生,并尝试注入基本结构指令来自Angular指南.这是我的指令:

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

我正在尝试将其注入我的TradeModule:

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

并在TradeComponent的模板中使用HTML的以下部分:

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

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

但是我得到了错误:

未捕获的错误:无法解析DropdownDirective的所有参数: ([对象对象],[对象对象],?).

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

Webstorm也是我的@Directive装饰器的基础,并说以下话:

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

角度:无法解析DropdownDirective中的所有参数 /home/commercialsuicide/Desktop/my-app/src/client/app/dropdown.directive/dropdown.directive.ts: ([对象对象],[对象对象],?)

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输入未使用:

需要说的是,我已经看到此答案,并且emitDecoratorMetadatatsconfig.json.

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.

非常感谢

推荐答案

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

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.

如果您不想注入items,请删除该参数.在任何情况下,您都不需要自己创建一个组件实例来显式地传递参数.

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天全站免登陆