Anthony O.
Anthony O.

Reputation: 24477

Create the side menu as a component in ionic 4

I created my ionic 4 application using ionic start myApp sidemenu.

Now I just want the side menu to be a component.

So I created:

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
})
export class MenuComponent implements OnInit {

  // [...] code copied from original generated app.component.ts

}
@NgModule({
  imports: [
    CommonModule,
    IonicModule,
    RouterModule
  ],
  declarations: [MenuComponent],
  exports: [MenuComponent]
})
export class MenuComponentModule {}
@NgModule({
  declarations: [AppComponent],
  entryComponents: [MenuComponent],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    MenuComponentModule
  ],
  // [...] providers & bootstrap identicall as original
})
export class AppModule {}
<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>

But then nothing is display and I have this error in console I don't know why:

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

Upvotes: 1

Views: 3452

Answers (1)

alex1983
alex1983

Reputation: 21

Please add the contentId and the id to your elements like this:

<ion-app>
  <ion-split-pane contentId="menu-content">
    <ion-menu contentId="menu-content" side="end">
      <app-menu></app-menu>
    </ion-menu>
    <ion-router-outlet id="menu-content"></ion-router-outlet>
  </ion-split-pane>
</ion-app>

Here is the menu component:

<ion-header>
  <ion-toolbar>
    <ion-title>Menu</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  <ion-list>
    <ion-menu-toggle auto-hide="false">
      <ion-item>Menu Item</ion-item>
    </ion-menu-toggle>
  </ion-list>
</ion-content>

and add the menu on the page you need it:

....
<ion-buttons slot="end">
  <ion-menu-button></ion-menu-button>
</ion-buttons>
....

hope this helps

Upvotes: 2

Related Questions