如何在页面中使用组件

Ama*_*oze 4 ionic-framework angular ionic4 angular7

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

  • 我创建了我的页面:ionic g pages name
  • 我创建了我的组件:ionic g component name

此时我只是尝试这个:

<ion-content>
    <app-menu-button></app-menu-button>
</ion-content>
Run Code Online (Sandbox Code Playgroud)

app-menu-button 是组件选择器

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

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

exports: [
    MenuButtonComponent
  ]
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

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

谢谢,

小智 7

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

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 {

}
Run Code Online (Sandbox Code Playgroud)

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