欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue項目keepAlive配合vuex動態(tài)設置路由緩存方式

 更新時間:2022年04月15日 16:52:00   作者:qlj224  
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ù)時間戳判斷對應的時間(今天 昨天 前天)

    這篇文章主要介紹了vue中 根據(jù)時間戳 判斷對應的時間(今天 昨天 前天),需要的朋友可以參考下
    2019-12-12
  • element-ui中table組件的toggleRowSelection()方法使用

    element-ui中table組件的toggleRowSelection()方法使用

    這篇文章主要介紹了element-ui中table組件的toggleRowSelection()方法使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue計算屬性實現(xiàn)成績單

    Vue計算屬性實現(xiàn)成績單

    這篇文章主要為大家詳細介紹了Vue計算屬性實現(xiàn)成績單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue實現(xiàn)自定義"模態(tài)彈窗"組件實例代碼

    vue實現(xiàn)自定義"模態(tài)彈窗"組件實例代碼

    頁面中會有很多時候需要彈窗提示,我們可以寫一個彈窗組件,下面這篇文章主要給大家介紹了關于vue實現(xiàn)自定義"模態(tài)彈窗"組件的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • Vue指令的學習

    Vue指令的學習

    這篇文章主要介紹了Vue指令,Vue官網(wǎng)一共有提供了14個指令,下面我們就來詳細講解每一個指令的詳細內容,需要的朋友可以參考一下
    2021-10-10
  • ant-design-vue 快速避坑指南(推薦)

    ant-design-vue 快速避坑指南(推薦)

    ant-design-vue是螞蟻金服 Ant Design 官方唯一推薦的Vue版UI組件庫,它其實是Ant Design的Vue實現(xiàn),組件的風格與Ant Design保持同步,組件的html結構和css樣式也保持一致,很多朋友在使用過程中遇到很多問題,今天小編就給大家分享一篇教程幫助大家快速閉坑,一起看看吧
    2020-01-01
  • Vue+Canvas制作簡易的水印添加器小工具

    Vue+Canvas制作簡易的水印添加器小工具

    隨著搬運工的逐漸增加,原創(chuàng)作者的利益收到了極大的影響。所以給圖片或視頻加上水印顯得極其重要。本文分享一個由canvas和vue.js制作的圖片水印添加器,需要的可參考一下
    2022-06-06
  • Vue路由跳轉傳參或打開新頁面跳轉的方法總結

    Vue路由跳轉傳參或打開新頁面跳轉的方法總結

    這篇文章主要給大家介紹了關于Vue路由跳轉傳參或打開新頁面跳轉的相關資料,在使用Vue.js開發(fā)單頁面應用時常常會遇到路由跳轉傳參的需求,需要的朋友可以參考下
    2023-07-07
  • Element Input組件分析小結

    Element Input組件分析小結

    這篇文章主要介紹了Element Input組件分析小結,詳細的介紹了Input組件的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • 茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒

    茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒

    這篇文章主要介紹了茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10

最新評論