this.$router.go() vs this.$router.push()

vue.js里面想通过点击按钮=》发送post请求=》根据返回值改router应该怎么做?

this.$http(...).then(reponse=>{
    this.$router.go({'/link-to-go'})
});

我发现结果没动,页面刷新了一下,表单被重置了,但没有跳转到link-to-go,而使用this.$router.push就可以

请问这两者的区别和应用环境

阅读 16.9k
5 个回答

如果是vue2

router.go(n)
这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)

router.push(location)
想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

router.replace(location)
跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。

router.push(location)

想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

router.go(n)

这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)。

https://router.vuejs.org/zh-c...

用window.location.reload()

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
1 篇内容引用
推荐问题