Vue.js 重定向到另一个页面 [英] Vue.js redirection to another page
问题描述
我想在 Vue.js
中进行类似于 vanilla javascript 的重定向
I'd like to make a redirection in Vue.js
similar to the vanilla javascript
window.location.href = 'some_url'
我如何在 Vue.js 中实现这一点?
How could I achieve this in Vue.js?
推荐答案
如果你使用的是 vue-router
,你应该使用 router.go(path)
来导航到任何特定路线.可以使用 this.$router
.
If you are using vue-router
, you should use router.go(path)
to navigate to any particular route. The router can be accessed from within a component using this.$router
.
否则,window.location.href = 'some url';
适用于非单页应用.
Otherwise, window.location.href = 'some url';
works fine for non single-page apps.
EDIT:router.go()
在 VueJS 2.0 中更改.您现在可以使用 router.push({ name: "yourroutename"})
或仅使用 router.push("yourroutename")
来重定向.
EDIT: router.go()
changed in VueJS 2.0. You can use router.push({ name: "yourroutename"})
or just router.push("yourroutename")
now to redirect.
P.S:在控制器中使用:this.$router.push({ name: 'routename' })
P.S: In controllers use: this.$router.push({ name: 'routename' })
这篇关于Vue.js 重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!