VUE keepAlive緩存問題之動態使用exclude(會使用到VUEX)


exclude是啥?

官方解釋:

 

 怎么用呢?

處理的問題是什么?(答:返回首頁的時候清除B頁面的緩存)

我遇到的問題是:

一開始狀態:A(首頁)、 B(列表)、C(列表中的詳情)三個頁面,設置B頁面的keepAlive為true;

操作順序:A=》B(1)=》C=》B=》A=》B=》 C=》B(4)

此時,最后一個B(4)頁面出現了B(2)中的緩存數據?(bug)

可是我已經清除緩存了,為啥還會這樣?

處理方法:

(1)使用vueX存儲全局變量;

 store.js

 1 import Vue from 'vue';
 2 import Vuex from 'vuex';
 3 import user from './store/user.js'
 4 Vue.use(Vuex);
 5
 7 export default new Vuex.Store({
 8     modules: {
 9       user
10     },
11     state: {
12         excludeXjlistpage:""
13     },
14     mutations: {
15         changeExclude(state,data){
16             state.excludePage= data ;//data就是需要清除緩存的頁面的name
17 }
18 },
19 });

 

 APP.vue

<template>
        <div>
            <keep-alive :exclude="this.$store.state.excludePage">
                <router-view v-if="$route.meta.keepAlive" ></router-view>
            </keep-alive>
            <router-view v-if="!$route.meta.keepAlive"></router-view>
        </div>
</template>

 B頁面

 1 beforeRouteEnter(to, from, next) {
 2     next((vm) => {
 3       if(from.name == "A頁面name"){
 4         vm.$store.commit('changeExclude','')//changeExclude是事件,''是傳進去的參數,store.js中的data
 5       }
 6     });
 7   },
 8   beforeRouteLeave(to, from, next) {
 9     if (to.name == "A頁面name") {
10       this.$store.commit('changeExclude','B頁面的name')
11     }
12     next();
13   },

別忘了在router.js中設置keepAlive為true

記錄一下花了我好久好久四處詢問總結處理的問題,希望也對你有幫助.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM