vue返回首頁后如何清空路由問題
vue返回首頁后如何清空路由
需求一:從首頁點擊路由到A頁面
- A頁面點擊路由到B頁面
- B頁面點擊路由到C頁面
- C頁面點擊路由鏈接到D頁面
- D頁面有個返回首頁按鈕
那么問題來了
點擊返回首頁后,再點擊手機(jī)的返回鍵 會打開D頁面 再按手機(jī)返回鍵 會打開C頁面,依次類推,
如何才能實現(xià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("點擊了瀏覽器的返回按鈕"); 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("點擊了瀏覽器的返回按鈕"); ? ? ? ? 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是返回的點擊事件 ? ? }
?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); ? }
vue路由緩存的靈活清空
vue的路由緩存,靈活清空,在頁面離開時判斷
beforeRouteLeave:function(to, from, next){ // 增加離開路由時清除keep-alive //rank寫在router.js的路由meta里面,用來判斷rank信息,可自由更改 if (from && from.meta.rank && to.meta.rank && from.meta.rank == to.meta.rank) {//此處判斷是如果返回上一層,你可以根據(jù)自己的業(yè)務(wù)更改此處的判斷邏輯,酌情決定是否摧毀本層緩存。 if (this.$vnode && this.$vnode.data.keepAlive) { if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) { if (this.$vnode.componentOptions) { var key = this.$vnode.key == null ? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '') : this.$vnode.key; var cache = this.$vnode.parent.componentInstance.cache; var keys = this.$vnode.parent.componentInstance.keys; if (cache[key]) { if (keys.length) { var index = keys.indexOf(key); if (index > -1) { keys.splice(index, 1); } } delete cache[key]; } } } } this.$destroy(); } next(); },
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- vue3中reactive的對象清空所引發(fā)的問題解決方案(清空不了和清空之后再去賦值就賦值不了)
- Vue.js實現(xiàn)輸入框清空功能的兩種方式
- vue前端更新后需要清空緩存代碼示例
- vue3清空reactive的四種方式
- Vue3如何清空Reactive定義的數(shù)組
- vue中el-date-picker type=daterange日期清空時不回顯的解決
- ant design vue 清空upload組件圖片緩存的問題
- vue如何實現(xiàn)清空this.$route.query的值
- vue清空form對象的方法
- vue3清空let?arr?=?reactive([])的實現(xiàn)示例
相關(guān)文章
vue中的data,computed,methods,created,mounted用法及說明
這篇文章主要介紹了vue中的data,computed,methods,created,mounted用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Vue實現(xiàn)當(dāng)前頁面刷新的五種方法總結(jié)
這篇文章主要介紹了Vue中實現(xiàn)頁面刷新的5種方法,包括使用$router.go(0)、location.reload()、通過router-view的key屬性、使用v-if指令手動觸發(fā)組件重新渲染,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02vue項目使用luckyexcel預(yù)覽excel表格功能(心路歷程)
這篇文章主要介紹了vue項目使用luckyexcel預(yù)覽excel表格,我總共嘗試了2種方法預(yù)覽excel,均可實現(xiàn),還發(fā)現(xiàn)一種方法可以實現(xiàn),需要后端配合,叫做KKfileview,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Vue基于iview table展示圖片實現(xiàn)點擊放大
這篇文章主要介紹了Vue基于iview table展示圖片實現(xiàn)點擊放大,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Vue.js組件props數(shù)據(jù)驗證實現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Vue.js組件props數(shù)據(jù)驗證的實現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10Vue中使用正則表達(dá)式進(jìn)行文本匹配和處理的方法小結(jié)
正則表達(dá)式在Vue中具有廣泛的應(yīng)用場景,包括文本匹配和處理、表單驗證等,通過本文的介紹和示例,希望讀者能更好地理解和應(yīng)用正則表達(dá)式在Vue中的使用方法,感興趣的朋友一起看看吧2023-11-11解決vue數(shù)據(jù)不實時更新的問題(數(shù)據(jù)更改了,但數(shù)據(jù)不實時更新)
這篇文章主要介紹了解決vue數(shù)據(jù)不實時更新的問題(數(shù)據(jù)更改了,但數(shù)據(jù)不實時更新),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10