I have a button on a tab page to reset the app for a user by removing a storage entry:
export class Tab1Page {
constructor(private router: Router, private storage: Storage, private toastController: ToastController) { }
async resetSettings() {
await this.storage.remove('welcomeComplete');
const toast = await this.toastController.create({
message: 'Your settings have been reset.',
duration: 2000
});
await toast.present();
console.log('before');
this.router.navigateByUrl('/');
console.log('after');
}
}
In the brower debugger, I can see that the entry is getting deleted from storage. I am also presented with the toast.
However, for some reason, the navigateByUrl method does not seem to be firing. The above pages sits at the url '/tabs/tab1'. Both console.log() statements are executed and there isn't an error in the console.
I'm pretty new to front end development, so apologies if this is a basic newbie question.
Update
my app-routing.module.ts
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { WelcomeGuard } from './welcome/welcome.guard';
const routes: Routes = [
{
path: '',
loadChildren: './tabs/tabs.module#TabsPageModule',
canActivate: [WelcomeGuard]
},
{
path: 'welcome',
loadChildren: './welcome/welcome.module#WelcomePageModule',
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { enableTracing: true, preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
my welcome.guard.ts
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router, CanActivate } from '@angular/router';
import { Observable } from 'rxjs';
import { Storage } from '@ionic/storage';
@Injectable({
providedIn: 'root'
})
export class WelcomeGuard implements CanActivate {
constructor(private router: Router, private storage: Storage) {}
async canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Promise<boolean> {
const welcomeComplete = await this.storage.get('welcomeComplete');
if (!welcomeComplete) {
this.router.navigateByUrl('/welcome');
}
return true;
}
}
I have changed my resetSettings() to the following:
async resetSettings() {
await this.storage.remove('welcomeComplete');
const toast = await this.toastController.create({
message: 'Your settings have been reset.',
duration: 2000
});
toast.onDidDismiss().then(() => {
this.router.navigateByUrl('');
});
await toast.present();
}
Changing resetSettings() didn't fix the issue.