亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue如何清除瀏覽器歷史棧

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

如何清除瀏覽器歷史棧

問(wèn)題

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

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

vue返回首頁(yè)后如何清空路由

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

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

那么問(wèn)題來(lái)了

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

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

mounted () {
? ? if (window.history && window.history.pushState) {
? ? ? ? // 向歷史記錄中插入了當(dāng)前頁(yè)
? ? ? ? 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)前頁(yè)
? ? ? ? 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);
? ? },
}?

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

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支付頁(yè)面到指定頁(yè)面?zhèn)鞯膮?shù) 來(lái)判斷他的路由
? ? ? if (this.$route.query.radio == 1 || this.$route.query.radio == 2) {
? ? ? ? this.$router.push({ //返回指定頁(yè)面
? ? ? ? });
? ? ? } else {
? ? ? ? this.$router.go(-1); ?// 正常返回
? ? ? }
? ? },
// 將事件清除掉
?destroyed() {
? ? window.removeEventListener("popstate", this.onClickLeft, false);
? }

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

相關(guān)文章

最新評(píng)論