【问题标题】:Redirect route with prepend in vuejs在 vuejs 中使用 prepend 重定向路由
【发布时间】:2020-08-12 16:50:45
【问题描述】:

我正在构建一个 Vuejs 应用程序,并且路由以这种方式工作:user_role/pageuser_role 在登录后根据您的角色而变化)并且当他们单击主页按钮时,我想将他们重定向到他们的根目录 /user_role 否不管是什么角色。

第一次尝试是在<router-view :to="homeRoute()"> 中添加一个计算函数,但这似乎并不理想。

第二次尝试是添加一个方法,这样:

 homeRoute() {
                this.$router.push(this.$route.matched[0].path)
            }

我收到此错误:

“不允许导航到当前位置(“/client/dashboard”)”。

/user_role/ 之后添加任何内容并重定向到该路由的理想方式是什么?

【问题讨论】:

  • 为什么不直接使用homeRoute() { return this.role_name }<router-link :to="homeRoute"></router-link>
  • role_name 会根据您的角色(/client、/manager 等)而变化。我使用了 role_name 但它不是实际的 url。

标签: vue.js vue-router


【解决方案1】:

为什么不根据 userType 重定向?

goHome : function(){
   const redirectTo = user.accountType === "client" ? {name:'client'} : {name:'manager'}}

   // Catch prevent error if push an active route
   router.push(redirectTo).catch(err => {})
}

【讨论】:

  • 嗯,这有点复杂。其中一位用户是管理员,因此他可以更改门户。当他点击主页按钮时,我希望他被重定向到他所在的门户的根目录。
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 2020-12-03
  • 2021-01-16
  • 2022-01-26
相关资源
最近更新 更多