在 Vue 实例内部,你可以通过 $router
访问路由实例。因此你可以调用 this.$router.push
。
想要导航到不同的 URL,则使用 router.push
方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。
当你点击 <router-link>
时,这个方法会在内部调用,所以说,点击 <router-link :to="...">
等同于调用 router.push(...)
。
//字符串 // this.$router.push(‘/bar‘) //字符串 router.push(‘/bar‘) //对象 // router.push({ path: ‘/bar‘ }) //命名的路由 // let userId = 10 // router.push({ path: `/user/${userId}`})
原文:https://www.cnblogs.com/caijinghong/p/13652985.html