vue返回首頁后如何清空路由問題
vue返回首頁后如何清空路由
需求一:從首頁點(diǎn)擊路由到A頁面
- A頁面點(diǎn)擊路由到B頁面
- B頁面點(diǎn)擊路由到C頁面
- C頁面點(diǎn)擊路由鏈接到D頁面
- D頁面有個(gè)返回首頁按鈕
那么問題來了
點(diǎn)擊返回首頁后,再點(diǎn)擊手機(jī)的返回鍵 會(huì)打開D頁面 再按手機(jī)返回鍵 會(huì)打開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);
? }vue路由緩存的靈活清空
vue的路由緩存,靈活清空,在頁面離開時(shí)判斷
beforeRouteLeave:function(to, from, next){
// 增加離開路由時(shí)清除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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue3中reactive的對(duì)象清空所引發(fā)的問題解決方案(清空不了和清空之后再去賦值就賦值不了)
- Vue.js實(shí)現(xiàn)輸入框清空功能的兩種方式
- vue前端更新后需要清空緩存代碼示例
- vue3清空reactive的四種方式
- Vue3如何清空Reactive定義的數(shù)組
- vue中el-date-picker type=daterange日期清空時(shí)不回顯的解決
- ant design vue 清空upload組件圖片緩存的問題
- vue如何實(shí)現(xiàn)清空this.$route.query的值
- vue清空form對(duì)象的方法
- vue3清空let?arr?=?reactive([])的實(shí)現(xiàn)示例
相關(guān)文章
vue中的data,computed,methods,created,mounted用法及說明
這篇文章主要介紹了vue中的data,computed,methods,created,mounted用法及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue實(shí)現(xiàn)當(dāng)前頁面刷新的五種方法總結(jié)
這篇文章主要介紹了Vue中實(shí)現(xiàn)頁面刷新的5種方法,包括使用$router.go(0)、location.reload()、通過router-view的key屬性、使用v-if指令手動(dòng)觸發(fā)組件重新渲染,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐
這篇文章主要介紹了基于Vue CSR的微前端實(shí)現(xiàn)方案實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
vue項(xiàng)目使用CDN引入的配置與易出錯(cuò)點(diǎn)
在日常開發(fā)過程中,為了減少最后打包出來的體積,我們會(huì)用到cdn引入一些比較大的庫來解決,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目使用CDN引入的配置與易出錯(cuò)點(diǎn)的相關(guān)資料,需要的朋友可以參考下2022-05-05
vue項(xiàng)目使用luckyexcel預(yù)覽excel表格功能(心路歷程)
這篇文章主要介紹了vue項(xiàng)目使用luckyexcel預(yù)覽excel表格,我總共嘗試了2種方法預(yù)覽excel,均可實(shí)現(xiàn),還發(fā)現(xiàn)一種方法可以實(shí)現(xiàn),需要后端配合,叫做KKfileview,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Vue基于iview table展示圖片實(shí)現(xiàn)點(diǎn)擊放大
這篇文章主要介紹了Vue基于iview table展示圖片實(shí)現(xiàn)點(diǎn)擊放大,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Vue.js組件props數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Vue.js組件props數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
Vue中使用正則表達(dá)式進(jìn)行文本匹配和處理的方法小結(jié)
正則表達(dá)式在Vue中具有廣泛的應(yīng)用場景,包括文本匹配和處理、表單驗(yàn)證等,通過本文的介紹和示例,希望讀者能更好地理解和應(yīng)用正則表達(dá)式在Vue中的使用方法,感興趣的朋友一起看看吧2023-11-11
解決vue數(shù)據(jù)不實(shí)時(shí)更新的問題(數(shù)據(jù)更改了,但數(shù)據(jù)不實(shí)時(shí)更新)
這篇文章主要介紹了解決vue數(shù)據(jù)不實(shí)時(shí)更新的問題(數(shù)據(jù)更改了,但數(shù)據(jù)不實(shí)時(shí)更新),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Vue中子組件調(diào)用父組件的3種方法實(shí)例
vue子組件調(diào)用父組件的方法其實(shí)不難,最近整理了一下,下面這篇文章主要給大家介紹了關(guān)于Vue中子組件調(diào)用父組件的3種方法,需要的朋友可以參考下2022-05-05

