【发布时间】: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