【问题标题】:Angular4 router.navigate doesn't do anythingAngular4 router.navigate 什么都不做
【发布时间】:2018-08-29 00:13:53
【问题描述】:

我有基于系统中客户角色的条件导航。像这样的:

this.GetQuickStartStatus()
                    .subscribe(data => {
                            if(data.isActive){
                                this.storageService.Store('isQuick', "true");
                            }
                            else{
                                this.storageService.Store('isQuick', "false");
                            }

                            if(this.storageService.Retrieve("Role") === "user"){
                                console.log("package");
                                this.router.navigate(['/packages']);
                            }

                            if(this.storageService.Retrieve("Role") === "subscriber" && this.storageService.Retrieve("isQuick") === "false"){
                                console.log("quickstart");
                                this.router.navigate(['/quickstart']);
                            }
                            console.log("nothing");
                            this.router.navigate(['']);
                    }, (error) => {
                        console.log(error);
                    });

我使用新用户登录,该用户应该重定向到包页面,因为用户角色是“用户。我在控制台中得到他:

auth.service.ts:156 包
auth.service.ts:164 什么都没有

含义条件被命中,但没有做任何事情,也就是路由器没有导航到请求的页面。我检查了所有条件都满足并且所有值都存储在本地存储中。为什么 router.navigate 会忽略重定向?

包的路由模块:

@NgModule({
  imports: [
    RouterModule.forChild([
        { path: 'packages', component: PackageComponent, canActivate: [AuthGuardService] },
        { path: 'receipt', component: PackageReceiptComponent, canActivate: [AuthGuardService, RoleSubscriberGuardService] },
        { path: 'receipt/:token', component: PackageReceiptComponent, canActivate: [AuthGuardService, RoleSubscriberGuardService]}
    ])
  ],
  exports: [RouterModule]
})
export class PackageRoutingModule { }

【问题讨论】:

  • 分享你的路由模块?
  • 问题不在此处:this.router.navigate(['']); ?每次都会执行此行。尝试表扬它或使用else
  • 我附加了包裹。我把它们隔离了
  • 您是否尝试过导航到“/”而不是“”?
  • @MaciejTreder 你能不能给出一个答案,就是这样,不确定它在第一次尝试时是如何工作的,可能是因为缓存。清理你的缓存人员。

标签: angular typescript


【解决方案1】:

您的方法末尾有this.router.navigate(['']); - 每次都会执行。

尝试将此行包装在else 块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    • 2014-05-11
    • 2014-09-10
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多