欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue如何清除瀏覽器歷史棧

 更新時間:2022年05月25日 10:51:31   作者:weixin_45108907  
這篇文章主要介紹了vue如何清除瀏覽器歷史棧,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

如何清除瀏覽器歷史棧

問題

需要跳轉(zhuǎn)好幾個頁面進(jìn)行表單提交,提交完之后,跳轉(zhuǎn)回首頁,返回上一頁,發(fā)現(xiàn)還可以返回上一級頁面路由

//可以拿到歷史記錄棧,清空棧
let routeHistory=history.length-1;
this.$router.go(-routeHistory);

vue返回首頁后如何清空路由

需求一:從首頁點(diǎn)擊路由到A頁面

  • A頁面點(diǎn)擊路由到B頁面
  • B頁面點(diǎn)擊路由到C頁面
  • C頁面點(diǎn)擊路由鏈接到D頁面
  • D頁面有個返回首頁按鈕

那么問題來了

點(diǎn)擊返回首頁后,再點(diǎn)擊手機(jī)的返回鍵 會打開D頁面 再按手機(jī)返回鍵 會打開C頁面,依次類推,

如何才能實(shí)現(xiàn)點(diǎn)擊返回首頁后,清空路由呢

mounted () {
? ? if (window.history && window.history.pushState) {
? ? ? ? // 向歷史記錄中插入了當(dāng)前頁
? ? ? ? history.pushState(null, null, document.URL);
? ? ? ? window.addEventListener('popstate', this.goBack, false);
? ? }
},
destroyed () {
? ? window.removeEventListener('popstate', this.goBack, false);
},
methods: {
? ? goBack () {
? ? ? ? // console.log("點(diǎn)擊了瀏覽器的返回按鈕");
? ? ? ? sessionStorage.clear();
? ? ? ? window.history.back();
? ? },
}?

禁止有返回記錄

mounted () {
? ? if (window.history && window.history.pushState) {
? ? ? ? // 向歷史記錄中插入了當(dāng)前頁
? ? ? ? history.pushState(null, null, document.URL);
? ? ? ? window.addEventListener('popstate', this.goBack, false);
? ? }
},
destroyed () {
? ? window.removeEventListener('popstate', this.goBack, false);
},
methods: {
? ? goBack () {
? ? ? ? // console.log("點(diǎn)擊了瀏覽器的返回按鈕");
? ? ? ? history.pushState(null, null, document.URL);
? ? },
}?

需求二:把瀏覽器的記錄返回指定的頁面

mounted 中:
?if (window.history && window.history.pushState) {
? ? ? history.pushState(null, null, document.URL);
? ? ? window.addEventListener("popstate", _this.onClickLeft, false); ?//_this.onClickLeft是返回的點(diǎn)擊事件
? ? }
?methods: {
? ? onClickLeft() {
? ? // ? this.$route.query.radio支付頁面到指定頁面?zhèn)鞯膮?shù) 來判斷他的路由
? ? ? if (this.$route.query.radio == 1 || this.$route.query.radio == 2) {
? ? ? ? this.$router.push({ //返回指定頁面
? ? ? ? });
? ? ? } else {
? ? ? ? this.$router.go(-1); ?// 正常返回
? ? ? }
? ? },
// 將事件清除掉
?destroyed() {
? ? window.removeEventListener("popstate", this.onClickLeft, false);
? }

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論