Open new tab in the same browser window with Vue

York Chen
2 min readDec 1, 2020

--

在逛電商網站時,行銷頁常會點選商品開新視窗,導致開啟一大堆擾人,又占用瀏覽器記憶體。
為了解決這個問題,可以用以下方式解決

宣告兩個變數

flag判斷現在是否已經開新視窗了,如果是ture就更新該視窗。
oldWindows存舊的router所開的新視窗

data() {
return {
flag:false,
oldWindows: "",
}
},

使用者點選開新視窗的function

<a @click="openTab($event,{name: 'xxxx', params: {yyy: id}})">

openTab function

如果第一次開新視窗,不用將舊的視窗關閉,以this.flag作為檢查機制
this.oldWindows存下開新視窗的function
第二次開以後,皆會先關掉之前this.oldWindows開的視窗,重新開啟

如此一來,使用者就不會一直開啟一堆視窗了

openTab(e,route) {     if(this.flag==false){
var routeData =this.$router.resolve(route);
this.oldWindows = window.open(routeData.href,'_blank');
this.flags=true;
}else{
this.oldWindows.close();
var routeDataNew =this.$router.resolve(route);
this.oldWindows = window.open(routeDataNew.href,'_blank');
this.flag=true;
}
},

reload,created時將flag更新為預設的false

created() { this.flag=false;

},

--

--

York Chen
York Chen

Written by York Chen

Cooking is an indispensable part of life. It is a taste that can’t be forgotten.

No responses yet