Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70443

Navigating from single page view to side-menu layout in ionic 4

$
0
0

@AndrewB wrote:

I’d like to build an app using Ionic 4 that simplistically works the following way:

  • On-boarding page is shown with some images/text
  • After user clicked ‘start’ button, some flag onboarded=true is written into local-storage
  • User redirected to the main app view which is a ion-split-pane containing side-menu layout
  • Next time when user launches app, I check if he/she already viewed on-boarding screen (by checking presence of onboarded var) and if it is - I immediately redirect the user to the main app having side-menu layout as I mentioned, omitting on-boarding screen

I started the project using ionic cli, based on side-menu template and modified it the following way to fulfill logic described above:

app.component.ts

import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Storage } from '@ionic/storage';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>',
})
export class AppComponent {

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private storage: Storage,
    private router: Router
  ) {
    this.initializeApp();
  }

  async initializeApp() {

    await this.platform.ready();

    this.statusBar.styleDefault();

    this.splashScreen.hide();

    const onboarded = await this.storage.get('onboarded');

    if (onboarded) {

      this.router.navigate(['main-app']);

    } else {

      this.router.navigate(['onboarding']);
    }
  }
}

onboarding.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>onboarding</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
Welcome aboard!
<ion-button (click)="start()">Start app!</ion-button>
</ion-content>

onboarding.page.ts

import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Router } from '@angular/router';

@Component({
  selector: 'app-onboarding',
  templateUrl: './onboarding.page.html',
  styleUrls: ['./onboarding.page.scss'],
})
export class OnboardingPage {

  constructor(
    private storage: Storage,
    private router: Router
  ) { }

  start() {

    this.storage.set('onboarded', true);

    this.router.navigate(['main-app']);
  }
}

main-app.page.html

  <ion-split-pane>
    <ion-menu>
      <ion-header>
        <ion-toolbar>
          <ion-title>Menu</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content>
        <ion-list>
          <ion-menu-toggle auto-hide="false" *ngFor="let p of appPages">
            <ion-item [routerDirection]="'root'" [routerLink]="[p.url]">
              <ion-icon slot="start" [name]="p.icon"></ion-icon>
              <ion-label>
                {{p.title}}
              </ion-label>
            </ion-item>
          </ion-menu-toggle>
        </ion-list>
      </ion-content>
    </ion-menu>
    <ion-router-outlet main></ion-router-outlet>
  </ion-split-pane>

main-app.page.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-main-app',
  templateUrl: './main-app.page.html',
  styleUrls: ['./main-app.page.scss'],
})
export class MainAppPage {

  public appPages = [
    {
      title: 'Home',
      url: '/home',
      icon: 'home'
    },
    {
      title: 'List',
      url: '/list',
      icon: 'list'
    }
  ];

  constructor() { }

}

List and Home pages are just empty pages and their content is not important for this example
Problem is that main-app containing side menu and content is not working properly. I know that there is a problem in <ion-router-outlet main></ion-router-outlet>'s usage but couldn’t find the right solution.
Does anyone have ideas how to fix it?

Thanks!
Andrew.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70443

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>