Angular 2 - Route guard not working on browser refresh

前端 未结 3 2066
被撕碎了的回忆
被撕碎了的回忆 2021-01-15 01:12

Having these routes defined in app.module.ts

...
{ path: \'mypath\', component: MyComponent, canActivate: [RouteGuard] }, 
{ path: \'\', redirectTo: \'/home\         


        
3条回答
  •  伪装坚强ぢ
    2021-01-15 01:37

    you can an asynchronous result to the route guard :

     canActivate(): Promise {
       let self = this;
        return this.auth.toPromise().then(auth => {
             if  (auth)  { self.loggedIn = true; }
             else { self.loggedIn = false; }
    
             self.router.navigate(['/login']); for future implememtation
    
            return self.loggedIn;
        });
    
    
    }
    

提交回复
热议问题