window.open和router.resolve区别
2022-04-21 本文已影响0人
秀萝卜
今天上班正好有时间,做一个简单总结
1.window.open(url)
在新页面打开url
window.open("www.baidu.com")
window.open("www.baidu.com","_blank") // 同上
window.open("") // 打开一个空页面
2.在location.href = url
在当前页面跳转url
location.hrerf = url
window.location.href = url // 同上
如果在vue中使用,必须加上https,咩有http不行
window.location.replace是替换当前页面,详看:https://blog.csdn.net/qq_34574204/article/details/109179873
3. this.$router.resolve实现页面跳转并传参(在新窗口打开页面)
需求:打开一个新的vue页面,并传参
分析:window.open能够打开新页面,但是参数必须在url中才可以。
我们可以使用this.$router.resolve来实现
const {href} = this.$router.resolve({
path: "/跳转的页面路由",
query:{//要传的参数
id:this.id
}
});
window.open(href, '_blank');//打开新的窗口
query的参数就是在url中的,效果和window.open是一样的,但是有的时候是param传参,就不一样了