Is it possible to use slides in ionic sidebar

前端 未结 1 1632
一生所求
一生所求 2021-01-26 09:54

I\'m building a mobile app in ionic and I wanna make a slack-like side menu by placing slides.

For example, when you click on main menu item, it will slide out another s

相关标签:
1条回答
  • 2021-01-26 10:35

    The ion-slides component makes use of the Swiper library under the hood. Part of the init code for Swiper depends on knowing the width of the slide container, and the code uses clientWidth to get it. Since the menu starts out with display:none, the retrieved width is always zero and the init code bails on you.

    You can get around this by temporarily setting display:block while Swiper initializes. I have my side menu inside of a component, so you may need to adjust this code to your situation:

    app.html:

    <sidebar [content]="content"></sidebar>
    <ion-nav #content [root]="rootPage" swipeBackEnabled="false"></ion-nav>
    

    sidebar.html:

    <ion-menu [content]="content" swipeEnabled="false">
      <ion-content>
        <ion-slides pager>
          <ion-slide>
            <h2>Slide 1</h2>
          </ion-slide>
          <ion-slide>
            <h2>Slide 2</h2>
          </ion-slide>
          <ion-slide>
            <h2>Slide 3</h2>
          </ion-slide>
        </ion-slides>
      </ion-content>
    </ion-menu>
    

    sidebar.component.ts:

    ...
    @Component({
      selector: 'sidebar',
      templateUrl: 'sidebar.html',
    })
    export class SidebarComponent implements AfterViewInit {
      @Input('content') content: NavController;
    
      @ViewChild(Slides) slides: Slides;
      @ViewChild(Menu, { read: ElementRef }) menuRef: ElementRef;
    
      // Use Renderer2 instead of direct DOM manipulation through the
      // ElementRef.nativeElement.
      //
      // @see: https://medium.com/@kmathy/angular-manipulate-properly-the-dom-with-renderer-16a756508cba
      //
      constructor(private renderer: Renderer2) {
      }
    
      // ViewChild template references might not be available until
      // AfterViewInit lifecycle hook runs.
      //
      // @see: https://blog.angular-university.io/angular-viewchild/
      //
      ngAfterViewInit() {
        this.renderer.setStyle(this.menuRef.nativeElement, 'display', 'block');
        setTimeout(() => {
          // Swiper init has its own ngAfterViewInit code that delays 300ms
          // before running. Don't remove the 'display' style until after that.
          this.renderer.removeStyle(this.menuRef.nativeElement, 'display');
        }, 310);
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题