【问题标题】:Redirection with vue-router in navigation guards在导航守卫中使用 vue-router 进行重定向
【发布时间】:2017-10-06 20:52:52
【问题描述】:

我正在使用vue.js,我希望能够将用户重定向到导航守卫beforeRouteEnter中的另一个 URL

这个想法是如果用户的当前城市不在 url 中并重定向它。

例如,如果您输入 london.example.com 就可以了。 如果你输入example.com 我希望用户被重定向到london.example.com

问题

window.location.replace好像没有效果

next() 在 URL 中附加响应而不是在前面。

代码

beforeRouteEnter (to, from, next) {
const subdomain = window.location.hostname.split('.')[0]
if (!subdomain) {
  axios.get('/api/getCity').then((response) => {
    if (response.data) {
      window.location.replace(response.data + '.localhost:8080')
      next({ path: 'http://' + response.data + '.localhost:8080' })
    }
  })
}

},

【问题讨论】:

  • window.location.href = response.data + '.localhost:8080' 呢?
  • 已经试过了,结果一样。注意:程序确实进入if

标签: vue.js vue-router


【解决方案1】:
window.location.href = 'http://' + response.data + '.localhost:8080'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2019-10-21
    • 2021-06-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多