最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用keep-alive后清除緩存的方法

 更新時(shí)間:2021年08月09日 15:51:51   作者:carlos.chang  
這篇文章主要給大家介紹了關(guān)于vue使用keep-alive后清除緩存的相關(guān)資料,這個(gè)問(wèn)題在我們?nèi)粘9ぷ髦薪?jīng)常會(huì)用到,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

什么是keepalive?

在平常開(kāi)發(fā)中,有部分組件沒(méi)有必要多次初始化,這時(shí),我們需要將組件進(jìn)行持久化,使組件的狀態(tài)維持不變,在下一次展示時(shí),也不會(huì)進(jìn)行重新初始化組件。

也就是說(shuō),keepalive 是 Vue 內(nèi)置的一個(gè)組件,可以使被包含的組件保留狀態(tài),或避免重新渲染 。也就是所謂的組件緩存

基本用法

<keep-alive>
    <component />  //你的組件
</keep-alive>

需求:從列表頁(yè)進(jìn)入詳情頁(yè),再返回列表頁(yè)時(shí)保留查詢條件,但在切換其他tab時(shí),清空查詢條件。

解決:保留查詢條件很簡(jiǎn)單,直接引入keep-alive,但是清除的話,vue本身沒(méi)有api直接清除,所以要單獨(dú)處理。

參考文章:http://aspedrom.com/5HD5

router/index,攔截路由并做處理:

beforeRouteLeave:function(to, from, next){
    // 增加離開(kāi)路由時(shí)清除keep-alive
    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();
},

同時(shí)在路由中添加meta:

{
    // 賬號(hào)列表
    path: '/account',
    name: 'account',
    component: () => import('../views/account/index.vue'),
    meta: { title: '賬號(hào)列表' ,rank:1.5}
  },
  {
    // 添加賬號(hào)
    path: '/accountadd',
    name: 'accountadd',
    component: () => import('../views/account/add.vue'),
    meta: { title: '添加賬號(hào)' ,rank:2.5}
  },
  {
    // 編輯賬號(hào)
    path: '/accountedit/:id',
    name: 'accountedit',
    component: () => import('../views/account/add.vue'),
    meta: { title: '編輯賬號(hào)' ,rank:2.5}
  },
  {
    // 角色列表
    path: '/role',
    name: 'role',
    component: () => import('../views/role/index.vue'),
    meta: { title: '角色列表' ,rank:1.5}
  },

總結(jié)

到此這篇關(guān)于vue使用keep-alive后清除緩存的文章就介紹到這了,更多相關(guān)keep-alive清除緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

常山县| 大城县| 肥西县| 常宁市| 应用必备| 齐齐哈尔市| 靖边县| 安国市| 壤塘县| 曲麻莱县| 迁安市| 桐乡市| 日喀则市| 天柱县| 聂荣县| 四平市| 新田县| 嘉义县| 泰宁县| 杭锦旗| 怀远县| 梁山县| 北流市| 昌图县| 左权县| 大兴区| 鄂尔多斯市| 石台县| 资源县| 烟台市| 久治县| 都兰县| 玉山县| 同江市| 内乡县| 丰都县| 娄烦县| 莒南县| 芜湖县| 新田县| 潞城市|