在导航守卫中使用 vue-router 重定向

Redirection with vue-router in navigation guards

我正在使用 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 = 'http://' + response.data + '.localhost:8080'