angular - 如何在页面中使用组件

标签 angular ionic-framework angular7 ionic4

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

  • 我创建了我的页面:ionic g 页面名称
  • 我创建了我的组件:ionic g 组件名称

此时我只是试试这个:

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

app-menu-button 是组件选择器

我收到此错误:1。如果“app-menu-button”是一个 Angular 组件,则验证它是该模块的一部分。

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

exports: [
    MenuButtonComponent
  ]

但它不起作用。

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

谢谢,

最佳答案

如果您使用延迟加载,则必须在页面模块中导入您的自定义组件。例如:如果您想在名为 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 文件中删除导入和声明。

关于angular - 如何在页面中使用组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55396290/

相关文章:

javascript - 无法在 Typescript 中调用函数内的函数

php - 配置 htaccess 以使用 Angular 和 PHP 路由

javascript - Angular2 中页面发出过多 HTTP 请求

ionic-framework - Ionic 2/Angular 2 路由 - 渐进式 Web 应用程序

html - 内容宽度在打印屏幕中不起作用

Angular 2 错误 : Attempt to use a destroyed view: detectChanges Error: Attempt to use a destroyed view: detectChanges at ViewDestroyedError

javascript - Angularjs - 调用 json Web 服务总是返回 404

javascript - 删除搜索栏中的搜索词时,搜索功能不起作用

javascript - 单击图标无法导航到新页面

组件 html 中带有 OR 的 Angular *ngIf 条件