簡體   English   中英

在 Angular 14 中,如何在獨立組件中包含子組件?

[英]In Angular 14, how do I include a child component in a standalone component?

我正在使用 Angular 14。我有一個獨立組件,我想在其中包含來自另一個模塊的子組件。 獨立組件看起來像

<div>
    <child-component></child-component>
</div>

它的服務文件是

@Component({
  selector: 'my-parent-component',
  templateUrl: './parent.component.html',
  standalone: true,
  imports: [
    MyModule
  ]
})
export class ParentComponent {
  ...
}

子組件在另一個模塊——MyModule 中。 my.module.ts 文件看起來像

/* imports */

@NgModule({
  declarations: [
    ChildComponent
  ],
  imports: [
    ...
  ]
})
export class MyModule { 
  constructor(entityDefinitionService: EntityDefinitionService) {
    ...
  }
}

但是我父母的 HTML 給了我錯誤

    <child-component></child-component>    

線...

'app-child-component' is not a known element:
1. If 'app-child-component' is an Angular component, then verify that it is included in the '@Component.imports' of this component.
2. If 'app-child-component' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@Component.schemas' of this component to suppress this message.

嘗試從MyModule導出ChildComponent ,以便您可以在其他地方使用它:

@NgModule({
  declarations: [
    ChildComponent
  ],
  imports: [
    ...
  ],
  exports: [ ChildComponent ]
})
export class MyModule { 
  constructor(entityDefinitionService: EntityDefinitionService) {
    ...
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM