【问题标题】:navigateByUrl from parent to child route not working从父到子路由的 navigateByUrl 不起作用
【发布时间】:2021-11-19 20:43:20
【问题描述】:

我会导航到我的应用程序中的一个子组件。

this.router.navigateByUrl(this.rightsService.pathToRedirectTo).then((data) => {
  console.log(data)
});

当我通过 /mainRoute 时它正在工作 路由:

{ path: 'mainRoute', loadChildren: () => import('./xxx/xxxx').then(m => m.xxxModule) },

但当我通过 /mainRoute/subRoute/1 时不重定向 ( data = false )

我的路由如下:

{ path: 'subRoute/:id', canActivate: [ViewGuard], component: ViewComponent },

我不知道为什么第二种情况不起作用?

这是我的父路由

{ path: 'login', component: LoginComponent },
{ path: 'mainRoute', loadChildren: () => import('./main/main.module').then(m => m.mainModule) },
{ path: '**', redirectTo: 'login' }];

然后在mainModule里面

我有路由:

{ path: 'subRoute/:id', canActivate: [ViewGuard], component: ViewComponent },
{ path: 'subroute/:id/childRoute/:sId', canActivate: [SViewGuard], component: SiewComponent }];

【问题讨论】:

    标签: javascript angular typescript angular-routing


    【解决方案1】:

    当您尝试重定向到/mainRoute/subRoute/1 时,请检查您的ViewGuard 返回什么,似乎路由守卫可以返回false,这可能是这个问题的原因,如果您使用任何拦截器这条路线或某些父组件,检查拦截器返回的值也是什么。

    【讨论】:

    • 感谢您的首次贡献。我已经删除了守卫,以确保错误不是来自这里。现在 navigateByUrl 返回 true 但不会将其重定向到参数中传递的路由。
    • 又出现了一个想法,可能路径/mainRoute/subRoute/1在app中不是绝对的。是否还有其他带有路由的父组件?您也可以检查一下,以确定路径有问题,只需尝试将此路由与其他方法 this.router.navigate(["/mainRoute/subRoute/1"]) 一起使用,如果它不会重定向,我建议检查您描述所有路由的父模块
    • 是的,如果我很好地理解了您的问题,我确实有父路由,我已将路由逻辑添加到我的问题中。
    猜你喜欢
    • 2016-06-13
    • 1970-01-01
    • 2023-04-06
    • 2019-08-11
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多