Vue.js 重定向到另一个页面 [英] Vue.js redirection to another page

查看:45
本文介绍了Vue.js 重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆