Ionic v4 menu only works when router-outlet has the main attribute

送分小仙女□ 提交于 2020-01-24 13:11:40

问题


I'm building an app with ionic v4 the first time and I want to include a side menu.

For testing reasons, I first put it inside of the app.component.html but I couldn't let it swipe out or in.

So I saw in the documentation, that they added the main attribute on the ion-router-outlet but as far as I read: it is not documented anywhere.

I don't know, why I'd have to add this, to make it actually work.

Source: https://ionicframework.com/docs/api/menu

<ion-router-outlet main></ion-router-outlet>

So I came across this: https://ionicframework.com/docs/api/router-outlet#properties where basically the component gets explained BUT

this property isn't documented at all. I can't find it.

So to everyone: Do I need to add this and when: why?

My menu is pretty basic but I print it out below, so you guys can see what i've build.

<ion-app>
  <ion-menu menuId="sideMenu" swipeGesture="true">
    <ion-header>
      <ion-toolbar>
        <ion-button slot="start">
          <img alt="logo" src="../assets/logo.svg">
        </ion-button>
      </ion-toolbar>
    </ion-header>
    <ion-content>
      <ion-list>
        <ion-item>Attack</ion-item>
        <ion-item>Defence</ion-item>
        <ion-item>Feedback</ion-item>
      </ion-list>
    </ion-content>
  </ion-menu>
  <ion-router-outlet main></ion-router-outlet>
</ion-app>

回答1:


The main attribute is a custom attribute that has no function by it self. Other components may rely on it. ion-menu will attach classes to the component that matches the contentId OR if this is not set with the element that has the main attribute.

You can see this by looking at the source code :

const content = this.contentId !== undefined
  ? this.doc.getElementById(this.contentId)
  : parent && parent.querySelector && parent.querySelector('[main]');

if (!content || !content.tagName) {
  // requires content element
  console.error('Menu: must have a "content" element to listen for drag events on.');
  return;
}
this.contentEl = content as HTMLElement;

// add menu's content classes
content.classList.add('menu-content');

To put it another way, you don't have to set the main attribute if you instead set an id on ion-content (just not the one inside ion-menu). You can also set the id on ion-router-outlet which will be the same thing as setting the main attribute.



来源:https://stackoverflow.com/questions/55341424/ionic-v4-menu-only-works-when-router-outlet-has-the-main-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!