如何在页面中使用组件

How to use Component in page

我正在尝试在多个页面中添加自定义组件。

此时我只是试试这个:

<ion-content>
    <app-menu-button></app-menu-button>
</ion-content>

app-menu-button 是组件选择器

我收到此错误:1. If 'app-menu-button' is an Angular component, then verify that it is part of this module.

我尝试在我的应用程序模块文件中添加:

exports: [
    MenuButtonComponent
  ]

但是不行。

我的目标是在多个页面中显示此组件。

谢谢,

所有组件都应该像下面这样在模块中声明。

@NgModule({
   declarations: [
      MenuButtonComponent
   ]
})

将您的组件添加到声明数组和入口组件数组中(如果它最初加载) declarations: [] && entryComponents: []

如果您使用延迟加载,则必须在页面模块中导入您的自定义组件。例如:如果您想在名为 ExamplePage 的页面中使用组件 AppMenuButtonComponent,则必须将其导入页面模块。

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ExamplePage } from './example';
import { AppMenuButtonComponent } from '../../components/appmenubutton/appmenubutton';

@NgModule({
  declarations: [
    ExamplePage,
    AppMenuButtonComponent
  ],
  imports: [
    IonicPageModule.forChild(ExamplePage),

  ],
  exports: [
    ExamplePage,
  ]
})
export class ExamplePageModule {

}

不要忘记从创建自定义组件时自动添加的 app.module.ts 文件中删除导入和声明。