【发布时间】:2019-07-27 10:01:00
【问题描述】:
我使用 ionic start myApp sidemenu 创建了我的 ionic 4 应用程序。
现在我只想让侧边菜单成为一个组件。
所以我创建了:
-
/src/app/menu/menu.component.ts:
@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
})
export class MenuComponent implements OnInit {
// [...] code copied from original generated app.component.ts
}
-
/src/app/menu/menu.component.html使用原始生成的代码app.component.html, -
/src/app/menu/menu.module.ts:
@NgModule({
imports: [
CommonModule,
IonicModule,
RouterModule
],
declarations: [MenuComponent],
exports: [MenuComponent]
})
export class MenuComponentModule {}
- 修改
/src/app/app.module.ts:
@NgModule({
declarations: [AppComponent],
entryComponents: [MenuComponent],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
MenuComponentModule
],
// [...] providers & bootstrap identicall as original
})
export class AppModule {}
- 将
/src/app/app.component.html修改为简单的这个文件:
<ion-app>
<ion-split-pane>
<ion-menu>
<app-menu></app-menu>
</ion-menu>
<ion-router-outlet main></ion-router-outlet>
</ion-split-pane>
</ion-app>
然后什么都没有显示,我在控制台中有这个错误我不知道为什么:
Error: Template parse errors:
'app-menu' is not a known element:
1. If 'app-menu' is an Angular component, then verify that it is part of this module.
2. If 'app-menu' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<ion-split-pane>
<ion-menu>
[ERROR ->]<app-menu></app-menu>
</ion-menu>
<ion-router-outlet main></ion-router-outlet>
"): ng:///AppModule/AppComponent.html@3:6
【问题讨论】:
-
你应该在你的module.ts文件中添加
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],,也可以查看这篇文章在ionic 4中创建一个侧边菜单petercoding.com/2019/05/05/side-menu-in-ionic4
标签: angular angular-components ionic4 angular-module