Soma
Soma

Reputation: 23

Ionic 4 home page load every time when i close and open app

When the user submits a name it saves in local storage in mobile and it redirects to the second page but the user closes and reopens the application it loads from the first page. below is my code.

This one is my Auth Guard code

 canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): boolean {
    if (localStorage.getItem('petData') != null) {
        return true;
    } else {
        this.route.navigateByUrl('/');
        return false;
    }
}

This one is routing

const routes: Routes = [
{
    path: 'home',
    loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
},
{
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
},
{
    path: 'dashboard',
    canActivate: [LocalGuardGuard],
    loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardPageModule)
},

];

This is the first page submit event.

    saveData() {
    // this.storage.set('petData', JSON.stringify(this.petData.petName));
    localStorage.setItem('petData', this.petData.petName);
    Swal.fire({
        // title: this.petData.petName,
        title: 'It is a not pet anymore, It is a ' + this.petData.petName,
        timer: 2000,
        timerProgressBar: true
    });
    this.router.navigateByUrl('/dashboard');
}

Upvotes: 1

Views: 309

Answers (0)

Related Questions