有一天域名被占用,领导要求把项目放在二级域名下,我心想这么简单,于是去改了router的base,没多久就部署了。随之而来的是铺天盖地的bug,我带着疑问看了代码,好家伙,n个window.open,window.open不会携带这个base值,难不成我要一个一个做生产环境判断去添加?
window.open('xxx','_blank');
于是领导就跟我说你重写一下window.open这个方法试一试。话不多说试一下!
window.open = function(_open) {
return function() {
const url = arguments[0]
if (url.startsWith('http')) {
_open.apply(this,arguments)
} else {
const newUrl = CONFIG.baseUrl + arguments[0];
arguments[0] = newUrl.replace(//{2}/g, '/') //是因为拼路径会出现双斜杠无奈只能去掉
_open.apply(this,arguments)
}
}
}(window.open)
果然有用嘿!然后我就查了资料发现还有一种也可以,需要一开始就写,不然后期维护有点抓狂
const newPages = this.$router.resolve({
path: `xxx`,
})
window.open(newPages.href,'_blank');