How can I conditionally load the route path? I've attempted the code below, but it's throwing an error. Can someone guide me on how to accomplish this task?
[ng] ERROR in Cannot read property 'loadChildren' of undefined [ng] i 「wdm」: Failed to compile.
Also:
app-routing.module.ts:21 Uncaught TypeError: Cannot read property 'getLandingPage' of undefined at Module../src/app/app-routing.module.ts (app-routing.module.ts:21) at webpack_require (bootstrap:83) at Module../src/app/app.module.ts (app.component.ts:21) at webpack_require (bootstrap:83) at Module../src/main.ts (main.ts:1) at webpack_require (bootstrap:83) at Object.0 (main.ts:13) at webpack_require (bootstrap:83) at checkDeferredModules (bootstrap:45) at Array.webpackJsonpCallback [as push] (bootstrap:32)
app.routing.module.ts
const routes: Routes = [
{
path: "",
redirectTo: this.getLandingPage(), // here is the issue
pathMatch: "full",
},
{
path: "tabs",
loadChildren: "./pages/tabs/tabs.module#TabsPageModule",
},
{
path: 'landing',
loadChildren: './pages/landing/landing.module#LandingPageModule'
},
];
export class AppRoutingModule {
getLandingPage(): string {
let url = "";
switch (environment.hotelName) {
case "h1":
url = "tabs";
break;
case "h2":
url = "landing";
break;
default:
}
return url;
}
}
I have auth.gurad.ts
as shown below. I don't think where I can use it for this.
export class AuthGuard implements CanActivate {
constructor(private router: Router,
private user: UserService,
private localStorageService: LocalStorageService) { }
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return new Promise(async resolve => {
const userInfo: UserInfo = await this.localStorageService.get(LocalStorage.USER_INFO);
if (userInfo && (moment() < moment(userInfo.expireDate))) {
this.user.guest = false;
return resolve(true);
}
this.user.guest = true;
this.router.navigate(["/sign-in"]);
return resolve(false);
});
}
}