1 history.pushState
<button @click="navTo('/kunln-mcm-web/system/updatePersonalInformation')">个人信息设置</button>
navTo(path) {
history.pushState(null, path, path);
},
与
window.open
方式相比,使用history.pushState
有以下优势:
- 用户体验更好:不刷新页面,仅更新URL,使页面过渡更平滑。
- 资源消耗更低:不会打开新的浏览器窗口或标签页,减少资源开销。
- 历史记录管理:更好地支持浏览器前进/后退功能,用户体验更一致。
相比之下,
window.open
会打开新窗口或标签页,可能导致资源浪费且不适合单页应用的场景。
2 this.$router.push()
this.$router.push('/login');
3 this.$router.resolve()
const url = router.resolve({ path: path, query: Object.assign({}, jparams) })
window.open(url.href, target)
具体应用场景
router.resolve: 当你需要生成一个完整的 URL 字符串,但不希望立即导航到该 URL 时,可以使用 router.resolve。 例如,生成一个分享链接或在新窗口中打开某个页面时,可以使用 router.resolve 获取完整的 URL。
router.push: 当你需要导航到一个新的页面或路由时,使用 router.push。 例如,用户点击某个按钮后,跳转到另一个页面。