【问题标题】:Navigating to a certain page if user enters an url from a new tab如果用户从新选项卡输入 URL,则导航到特定页面
【发布时间】:2017-11-08 11:51:26
【问题描述】:

我已经构建了一个 loginPageGuard,如果用户已经登录,它根本不会让用户登录页面。

假设用户正在使用

http://localhost:4200/#/testUrl

并尝试前往 http://localhost:4200/#/login

LoginPageGuard 可以按我的意愿工作,并且用户保持在

http://localhost:4200/#/testUrl

问题是当用户登录并打开一个新选项卡并在浏览器中输入以下 URL 时

http://localhost:4200/#/login

应用程序不让用户登录页面,而是去

http://localhost:4200/#/

我想要的是带给用户

http://localhost:4200/#/someDesiredUrl 

有什么办法可以做到这一点吗?谢谢

LoginPageGuard

@Injectable()
export class LoginPageGuard implements CanActivate {


  constructor(private router: Router, private authenticationService: AuthenticationService) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {

    return this.authenticationService.isAuthorized()
      .map((user:EUser)=>{
      return !user.isAuthorized;
    })
  }
}

app.routing.ts

  {
    path: 'login',
    canActivate:[LoginPageGuard],
    component: LoginComponent
  }

【问题讨论】:

    标签: angular routes angular-ui-router canactivate


    【解决方案1】:

    只需使用注入的路由器实例导航到someDesiredUrl

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
          //Authentication logic here
          //If already logged in
          this.router.navigateByUrl('someDesiredUrl');
    }
    

    【讨论】:

    • 如果用户打开一个新标签并尝试进入登录页面,我只希望用户转到某个所需的 URL。否则系统工作正常..
    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    相关资源
    最近更新 更多