vue項目keepAlive配合vuex動態(tài)設置路由緩存方式
需求
首頁 → 列表頁→ 詳情頁(緩存列表頁面 ) → 列表頁(不重新加載列表頁)→ 首頁(清除列表頁的緩存)
效果圖
解決方案
直接使用keepAlive會出現(xiàn)一個問題,當從查詢1進入列表頁面,這時緩存list組件,然后返回首頁,點擊查詢2,會發(fā)現(xiàn)list的數(shù)據(jù)是查詢1的,因為這里直接調用了上一次的緩存因此,在返回首頁后需清除list的緩存,下次進入list將重新初始化。
利用keepAlive進行路由緩存,keepAlive的include 和 exclude 屬性允許組件有條件地緩存。
配合vuex維護一個緩存數(shù)組即可,不多BB,直接上代碼
1.App.vue文件
利用include屬性進行選擇性緩存
<template> ? <div style="height: 100%;"> ? ? <keep-alive :include="$store.state.common.cachedRouteNames"> ? ? ? <router-view /> ? ? </keep-alive> ? </div> </template>
<script> export default { ? name: 'App' }; </script>
2.main.js文件
配置路由keepAlive狀態(tài)
import Vue from 'vue'; // import Vue from 'vue/dist/vue.esm.js' import App from '../src/App.vue'; import router from '../src/router/index'; // import "../src/assets/style/reset.css"; import 'lib-flexible'; import utils from './utils/utils'; import store from './store/index'; // 配置路由keepAlive狀態(tài) utils.setRouterBeforeEach(router); // runtime模式 new Vue({ router, store, render: h => h(App) }).$mount('#app');
3.store/modules/common.js文件
vuex管理緩存數(shù)組,編寫添加和刪除緩存路由方法
const UPDATE_CACHEDROUTENAMES = 'update_cachedroutenames'; const state = { activatedReloadData: true, // 頁面activated時是否需要重新加載 // 緩存的路由列表 cachedRouteNames: [] }; const mutations = { [UPDATE_CACHEDROUTENAMES](st, { action, route }) { const methods = { add: () => { st.cachedRouteNames.push(route); }, delete: () => { st.cachedRouteNames.splice(st.cachedRouteNames.findIndex(e => e === route), 1); } }; methods[action](); } }; export default { namespaced: true, state, mutations };
4.utils/utils.js文件
配置路由keepAlive狀態(tài)
import store from '../store/index'; const { cachedRouteNames } = store.state.common; const changeRoutes = (route, type) => { const routeName = route.components.default.name; if (routeName && type === 'add' ? !cachedRouteNames.includes(routeName) : cachedRouteNames.includes(routeName)) { store.commit('common/update_cachedroutenames', { action: type, route: routeName }); } }; // 定義添加緩存組件name函數(shù),設置的時組件的name const addRoutes = (route) => { changeRoutes(route, 'add'); }; // 定義刪除緩存組件name函數(shù),設置的是組件的name const deleteRoutes = (route) => { changeRoutes(route, 'delete'); }; // 配置路由keepAlive狀態(tài) const setRouterBeforeEach = (router) => { router.beforeEach((to, from, next) => { // 對該組件是否讀取緩存進行處理 to.matched.forEach((item) => { const routes = item.meta.cachedRouteNames; if (item.meta.keepAlive && (!routes || (routes && (!from.name || routes.includes(from.name))))) { addRoutes(item); } else { deleteRoutes(item); } }); next(); }); // 全局混入。在該組件被解析之后,若是屬于需要緩存的組件,先將其添加到緩存配置中,進行緩存 Vue.mixin({ beforeRouteEnter(to, from, next) { next(() => { to.matched.forEach((item) => { if (to.meta.keepAlive) { addRoutes(item); } }); }); }, }); }; export default { setRouterBeforeEach };
5.store/index.js文件
import Vue from 'vue'; import Vuex from 'vuex'; import actions from './actions'; import mutations from './mutations'; import state from './state'; import getters from './getters'; import app from './modules/app'; import common from './modules/common'; Vue.use(Vuex); const store = new Vuex.Store({ modules: { app, common }, state, mutations, actions, getters }); export default store;
6.router/index.js文件
keepAlive
:設置緩存cachedRouteNames
:設置緩存條件
// An highlighted block import Vue from 'vue'; import VueRouter from 'vue-router'; Vue.use(VueRouter); const Home = resolve => require(['../routers/Home.vue'], resolve); const List = resolve => require(['../routers/list.vue'], resolve); const Detail = resolve => require(['../routers/detail.vue'], resolve); const router = new VueRouter({ routes: [ { name: 'Home', path: '/home', component: Home }, { name: 'List', path: '/list', component: List, meta: { keepAlive: true, // 緩存條件:從List --> Detail 則緩存List cachedRouteNames: ['Detail'] } }, { name: 'Detail', path: '/detail', component: Detail } ] }); export default router;
7.routers/Home.vue文件
export default { name: 'Home', components: { HeaderBar }, data() { return { list: [ '查詢1', '查詢2' ] }; }, created() { // this.getData(); // console.log(111111); }, }; </script>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue中根據(jù)時間戳判斷對應的時間(今天 昨天 前天)
這篇文章主要介紹了vue中 根據(jù)時間戳 判斷對應的時間(今天 昨天 前天),需要的朋友可以參考下2019-12-12element-ui中table組件的toggleRowSelection()方法使用
這篇文章主要介紹了element-ui中table組件的toggleRowSelection()方法使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue實現(xiàn)自定義"模態(tài)彈窗"組件實例代碼
頁面中會有很多時候需要彈窗提示,我們可以寫一個彈窗組件,下面這篇文章主要給大家介紹了關于vue實現(xiàn)自定義"模態(tài)彈窗"組件的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2021-12-12茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒
這篇文章主要介紹了茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10