首页 > 解决方案 > 如何在页面中使用组件

问题描述

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

在这一点上,我只是试试这个:

<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
  ]

但它不起作用。

我的目标是在几个页面中显示这个组件。

谢谢,

标签: angularionic-frameworkangular7ionic4

解决方案


如果您使用延迟加载,则必须在页面模块中导入您的自定义组件。例如:如果你想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 {

}

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


推荐阅读