Angular 2 dynamically change default route

后端 未结 1 1154
逝去的感伤
逝去的感伤 2020-12-17 16:24

I need to allow the users of my application to change the default route when they want: I have a parameter page where they can select the \"page\" they want to show first wh

相关标签:
1条回答
  • 2020-12-17 17:03

    Actually you can use guards for this to redirect to correct url before navigation happens:

    { path: '', canActivate: [UserSettingsGuard], redirectTo: 'Day', pathMatch: 'full' }
    

    And you guard can looks like this:

    @Injectable()
    export class UserSettingsGuard implements CanActivate  {
      constructor(private router: Router) { }
    
      canActivate() : boolean {
        var user = ...;
    
        if(user.defaultPage) {
          this.router.navigate([user.defaultPage]);
        } else {
          return true;
        }
      }
    }
    

    So you can switch to new url when user with overriden page exists or use default flow instead.

    0 讨论(0)
提交回复
热议问题