Filevalidation 指令声明在两个不同的模块中不起作用

Filevalidation directive declaration not working in two different module

Uncaught Error: Type FileValueAccessor is part of the declarations of 2 modules: moduleX and moduleY Please consider moving FileValueAccessor to a higher module that imports moduleX and moduleY. You can also create a new NgModule that exports and includes FileValueAccessor then import that NgModule in moduleX and moduleY.

如果我在 app.module 中声明了 FileValueAccessor 和两个模块,那么它正在阅读另一个模板错误。

ERROR DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

如果我在不同的模块中声明使用文件验证的组件,它在 app.module 上工作。

为什么我需要在 app.module 中声明那些组件?我已经有两个使用延迟加载的不同模块。

尝试使用两个模块的导入导出组件,但它也需要工作:(

To solve this problem "Type FileValueAccessor is part of the declarations of 2 modules"

我们需要创建 shared/Shared.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { FileValueAccessor } from './validation/file-control-value-accessor';
import { FileValidator } from './validation/file-input.validator';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule
  ],
  exports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule,
    FileValueAccessor,
    FileValidator
  ],
  declarations: [
    FileValueAccessor,
    FileValidator
  ],
  providers: [
  ]
})
export class SharedModule { }

并将这个shared.module.ts文件导入到moduleX和moduleY。这将解决问题。