Angular @Optional指令注入被忽略



我收到这样的错误

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

有服务声明。

    constructor(
        @Optional() @Host() private form: FormSubmitDirective,
        private control: NgControl,
        private resolver: ComponentFactoryResolver,
        public viewContainerRef: ViewContainerRef,
        @Inject(FORM_ERRORS) private errors,
        @Optional() controlErrorContainer: ControlErrorContainerDirective
      ) {

我看到注射问题与可选的指令服务有关。是的,指令未设置为组件。但是它们是可选的。为什么要调用错误?是的,我向模块提供了所有指令。

@NgModule({
  declarations: [
    AppComponent,
    ControlErrorsDirective,
    ControlErrorContainerDirective,
    FormSubmitDirective,
    ControlErrorComponent
  ],
  entryComponents: [
    ControlErrorComponent
  ],

但它行不通。错误似乎微不足道。但是我看不到。在这里运行良好。https://stackblitz.com/edit/angular-terms-and-conditions-reactive-forms?embed = 1&file=src/app/app/form-form-erfor-rors.ts问候。

类似的错误:

无法解析某物的所有参数:(?,

通常意味着在执行装饰器时,用于构造函数中用于参数的类型是未定义的。

有几个原因可能发生:

  • 您在DI

  • 中使用的类之间有循环依赖性
  • 您在一个文件中都有所有类,并在声明之前尝试使用类型。订单很重要。

在您的情况下,您在一个文件中写了所有类,但是在ControlErrorsDirective类之前尝试使用FormSubmitDirectiveControlErrorContainerDirective,这是您问题的主要原因。

最新更新