【问题标题】:Angular 2: Redirect to the right routeAngular 2:重定向到正确的路线
【发布时间】:2017-02-26 13:11:06
【问题描述】:

我有 2 个模型:AppModule、PanelModule。

在我的 AppComponent 中,我检查用户是否登录,如果是,我将他重定向到面板。我就是这样做的:

ngOnInit() {
      //If he is alredy logged, redirect him to the panel
      this.authService.login().subscribe(() => {
          this.authService.loaded = true;
      if (this.authService.isLoggedIn) {
          console.log("Navigating to panel..");
        this.router.navigate(['/panel']);
      }
    });
  }

当我的位置是子路线时,问题就开始了。例如:

/panel/users

如果我尝试直接访问 URL,我会导航回 /panel,因为用户已登录并执行了 this.router.navigate(['/panel']);

什么是正确的方法,以便我的应用将用户重定向到正确的路径?

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    您可能只想订阅第一个结果:

    this.authService.login().first().subscribe(() => {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 2021-07-07
      • 2016-07-07
      • 2017-07-27
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多