我试图声明一个自定义组件以在(我的离子应用程序的)两个不同页面中使用,但是出现错误

时间:2018-09-13 10:07:59

标签: ionic-framework ionic3 components

我试图在ionic v3应用程序的两个不同页面中使用一个名为UserCardComponent的自定义组件。

我遵循了这个答案(一个26票的答案),说要在“特定页面的module.ts文件”中声明自定义组件。 Custom component in ionic v3
因此,我首先在HomePageModule中声明了该组件,并能够在home.html中很好地使用它。

home.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { HomePage } from './home';
import { UserCardComponent } from '../../components/user-card/user-card';

@NgModule({
  declarations: [
    HomePage,
    UserCardComponent
  ],
  imports: [
    IonicPageModule.forChild(HomePage)
  ],
})
export class HomePageModule {}

然后我尝试在ContactsPageModule中以相同的方式声明它,以便在contacts.html中使用,但是出现以下错误:

Type UserCardComponent is part of the declarations of 2 modules: HomePageModule and ContactsPageModule! 
Please consider moving UserCardComponent to a higher module that imports HomePageModule and ContactsPageModule. 
You can also create a new NgModule that exports and includes UserCardComponent then import that NgModule in HomePageModule and ContactsPageModule

当我尝试仅在app.module.ts文件中声明UserCardComponent时,出现模板解析错误,并且自定义组件在任何页面中均不起作用。

您能建议我做什么吗?该错误表明将“ UserCardComponent移至导入HomePageModule和ContactsPageModule的更高模块。”
你能告诉我我会怎么做吗?我是离子新手。谢谢

0 个答案:

没有答案