ionic4自定义组件报错'ion-' is not a known element:

项目要把顶部返回按钮抽成单个组件,于是使用ionic g component xx/xx/back 生成了组件文件
back.component.html中使用了ion-buttons

<ion-buttons slot="start">
  <ion-back-button text="返回" color="dark" icon="arrow-back">
  </ion-back-button>
</ion-buttons>

然后在公共的sharedModule里引入了这个组件:

@NgModule({
  declarations: [BackComponent],
  imports: [CommonModule],
  exports: [BackComponent],
})
export class SharedModule {}

结果一直报错:

Uncaught Error: Template parse errors:
'ion-back-button' is not a known element:
1. If 'ion-back-button' is an Angular component, then verify that it is part of this module.
2. If 'ion-back-button' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("<ion-buttons slot="start">
  [ERROR ->]<ion-back-button text="返回" color="dark" icon="arrow-back">
  </ion-back-button>
</ion-buttons>
......

这是因为没引入IonicModule引起的,在sharedModule里引入就好了:

@NgModule({
  declarations: [BackComponent],
  imports: [CommonModule, IonicModule],
  exports: [BackComponent],
})
export class SharedModule {}
发布了138 篇原创文章 · 获赞 168 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/qq_24734285/article/details/89260048