[英]Use custom component from imported module
我創建了一個名為MultipleIdentifier
的組件,使用formControlName
以反應形式使用。 它在名為Module1
的模塊中使用,並在該模塊的declarations
數組中聲明。 它運作良好。
現在,在同一個應用程序中,我想在另一個名為Module2
的模塊中使用這個組件。 這個組件被移動到名為Components.module
的模塊,這個新模塊被導入到app.module
中,因此它可以被Module1
和Module2
這兩個模塊使用。 它是這樣導出的:
@NgModule({
declarations: [
MultipleIdentifier,
],
imports: [
...
],
exports: [
MultipleIdentifier,
]
})
export class Components {
}
它編譯正常,但現在, Module1
和Module2
都沒有顯示該組件,並且都顯示錯誤: No value accessor for form control with name:
multipleIdentifierInput (where multipleIdentifierInput is my
MultipleIdentifier` 組件)。 我的組件正在使用這樣的期望提供者:
@Component({
selector: 'multiple-identifier',
templateUrl: './multiple-identifier.component.html',
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: forwardRef(() => MultipleIdentifier)
},
]
})
export class MultipleIdentifier implements ControlValueAccessor, Validator {
...
}
我錯過了什么? 似乎該組件已“識別”但不能再用作輸入字段的反應。
剛找到這個文檔: https://angular.io/guide/sharing-ngmodules我不得不將Components
模塊導入Module1
和Module2
並導入FormsModule
。 現在一切正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.