【问题标题】:router.navigate is not working in Angularrouter.navigate 在 Angular 中不起作用
【发布时间】:2018-01-14 17:34:24
【问题描述】:

下面是我的重定向方法,它不起作用

    goToProdPage() {
       this.router.navigate([this.quoteId,'/cpq']);
       window.location.reload();
    }

但是如果我把它改成

goToProdPage() {
     this.router.navigate(['./'+this.quoteId+'/cpq']);
     window.location.reload();
}

然后它工作正常。但现在我无法从其他组件的激活路由中获取 url 参数(即 quoteId)。

下面是 app.module.ts 中的路由代码

const appRouter :Routes = [
   {path:'login', component: loginPage},
   {path:':quoteId/cpq', component: cpqPage},
   {path:'', redirectTo:'/login', pathMatch:'full'},
]

【问题讨论】:

  • 为什么要调用window.location.reload?
  • 你为什么要构建字符串?传递数组的意义在于你可以做this.router.navigate([this.quoteId, 'cpq'])之类的事情,让路由器自己操心细节。
  • 因为在 window.location.reload 之前它只改变了 url 而不是内容。所以我强制重新加载页面,以便我可以转到重定向位置
  • @jonrsharpe 但是当我将它作为数组元素传递时它不起作用。这就是我需要你帮助的方式
  • 然后显示,提供minimal reproducible example,它实际上会重现问题。

标签: javascript angular angular2-routing angular4-router


【解决方案1】:

您不需要 window.location.reload(); 它将跳过缓存并重新加载同一页面,将其更改为

goToProdPage() {
    this.router.navigate([this.quoteId, 'cpq']);   
}

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 2017-09-28
    • 2019-04-06
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2019-09-08
    • 2020-05-21
    相关资源
    最近更新 更多