vue頁面緩存問題,開啟時(shí)緩存頁面,關(guān)閉時(shí)關(guān)閉緩存,再開啟時(shí)刷新原有緩存頁面

1、路由

????????<keep-alive>

? ? ? ? ? <router-view v-if="$route.meta.notCache"></router-view>

? ? ? ? ? </keep-alive>

? ? ? ? ? <router-view v-if="!$route.meta.notCache"></router-view>

2、數(shù)據(jù)狀態(tài)

? ? ? ?{

? ? path: '/ST01',

? ? name: 'ST01',

? ? component: Main,

? ? children: [{

? ? ? path: '/ST01',

? ? ? meta: {

? ? ? ? moduleCode: '001',

? ? ? ? hideInMenu: true,

? ? ? ? title: '學(xué)員列表',

? ? ? ? notCache: true? ? ? ? ? ? //? 這里是否緩存

? ? ? },

? ? ? name: 'ST01',

? ? ? component: () => import('../views/pages/ST01.vue')

? ? }]

? }

3、在需要關(guān)閉頁面時(shí),同時(shí)關(guān)閉緩存的情況下,

4、然后對應(yīng)的每個(gè)需要緩存頁面中

beforeRouteEnter(to, from, next){

? //設(shè)置頁面緩存

to.meta.notCache = true;

next();

? },

? beforeRouteLeave(to, from, next) {

//刪除頁面緩存

//?from.name是路由中name,也就是頁面的路由名

if (from.name === 'ST01' && !from.meta.notCache) {

if(this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache){

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();

? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基礎(chǔ)部分 模版語法 1.computed和watch的區(qū)別 計(jì)算屬性computed :支持緩存,data數(shù)據(jù)不變...
    王蕾_fd49閱讀 678評論 0 0
  • 一、props 這個(gè)應(yīng)該非常屬性,就是父傳子的屬性; props 值可以是一個(gè)數(shù)組或?qū)ο? //數(shù)組:不建議使用p...
    魂斗羅小黑閱讀 684評論 0 1
  • 前言 Vue基本用法很容易上手,但是有很多優(yōu)化的寫法你就不一定知道了,本文從列舉了 36 個(gè) vue 開發(fā)技巧; ...
    阿_軍閱讀 1,560評論 0 1
  • 在線訪問手冊:https://hanxueqing.github.io/Web-Front-end-Intervi...
    視覺派Pie閱讀 2,486評論 0 5
  • 推薦指數(shù): 6.0 書籍主旨關(guān)鍵詞:特權(quán)、焦點(diǎn)、注意力、語言聯(lián)想、情景聯(lián)想 觀點(diǎn): 1.統(tǒng)計(jì)學(xué)現(xiàn)在叫數(shù)據(jù)分析,社會...
    Jenaral閱讀 5,967評論 0 5

友情鏈接更多精彩內(nèi)容