Angular 4 directive error: Can't resolve all p

2020-07-09 07:46发布

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

enter image description here

It also say that my pcDropdown input is unused:

enter image description here

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

3条回答
老娘就宠你
2楼-- · 2020-07-09 07:53

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.

查看更多
迷人小祖宗
3楼-- · 2020-07-09 07:59

I just left my case here, because found just this question by my request.

I had annotation @HostListener and onResize method. @HostListener should be before and close to the related method. Like

@HostListener("window:resize", ["$event"])
onResize(event) {
    // some code
}

I got this error when had moved annotation before calling another method. Like

@HostListener("window:resize", ["$event"])
getSomeObjects (args:any = {}) {
    // some code
}

onResize(event) {
    // some code
}

Hope, this will be helpful for somebody!

查看更多
你好瞎i
4楼-- · 2020-07-09 08:00

I had this error, in my case was because I have a service named Storage, but the compiler won't mark it as error, because javascript have a global variable named the same. So I just added:

import { Storage } from '@core/auth/token-storage.service';
查看更多
登录 后发表回答