vue3.0列表頁面做緩存的方法代碼
一.設置動態(tài)keepalive
<router-view v-slot="{ Component, route }">
<keep-alive :include="cacheViewsState">
<component :is="Component" />
</keep-alive>
</router-view>可以將要緩存的頁面作為vuex全局變量儲存
const cacheViewsState = store.state.app.cachedViews
將cachedViews 存入vuex:
state: {
cachedViews: ['listPage']
},
mutations:{
ADD_CACHED_VIEW: (state, view) => {
if (state.cachedViews.includes(view)) return
state.cachedViews.push(view)
},
DEL_CACHED_VIEW: (state, view) => {
const index = state.cachedViews.indexOf(view)
index > -1 && state.cachedViews.splice(index, 1)
}
},
actions: {
//添加緩存組件
addCachedView({ commit }, view) {
commit('ADD_CACHED_VIEW', view)
},
//刪除緩存組件
delCachedView({ commit, state }, view) {
return new Promise((resolve) => {
commit('DEL_CACHED_VIEW', view)
resolve([...state.cachedViews])
})
}
}二.頁面初始化數(shù)據(jù)緩存處理
將頁面導出命名空間設置為動態(tài)緩存頁面名單
<script>
export default {
name: 'listPage'
}
</script>onActivated 注冊一個回調函數(shù),若組件實例是 <KeepAlive> 緩存樹的一部分,當組件從 DOM 中被移除時調用。
這個鉤子在服務器端渲染期間不會被調用。
onActivated(() => {
getList() // 初始化列表
})附:Vue3.0使用keep-alive實現(xiàn)頁面緩存不刷新
1.應用場景
1.列表頁進入詳情頁,再從詳情頁返回列表頁;列表頁緩存不刷新。保持原來選中的查詢參數(shù)以及當前頁
2.某個新增頁面分為兩步,分為A頁面和B頁面;當?shù)谝徊紸頁面信息填好后,點擊下一步到第二步B頁面。再返回到第一步A頁面,A頁面信息不丟失。同理第二步填好信息返回到第一步,再回到第二頁,第二頁頁面信息不丟失。
2.解決步驟
1.App.vue
//isRouterAlive:通過先設置isRouterAlive為false再設置為true可實現(xiàn)組件的銷毀
<router-view v-slot="{ Component }" v-if="isRouterAlive">
<keep-alive>
<component :is="Component" v-if="_this.$route.meta.keepAlive" :key="$route.name" />
</keep-alive>
<component :is="Component" v-if="!_this.$route.meta.keepAlive" />
</router-view>2.router.js
//設置meta
const routes: Array<RouteRecordRaw> = [
{
path: 'list',
name: 'list',
meta: { keepAlive: true, cacheList: ['detail'] },
component: () => import('@/views/list.vue')
},
]
//路由攔截
router.beforeEach((to, from, next) => {
//從cacheList中的任何一個頁面返回,當前頁面緩存
const cacheList: any = to.meta.cacheList
if (cacheList) {
if (cacheList.indexOf(from.name) > -1) {
to.meta.keepAlive = true
} else {
//解決第一次不緩存問題
if (from.name) {
to.meta.keepAlive = false
} else {
to.meta.keepAlive = true
}
}
}
next()
}3.list.vue
import { defineComponent, nextTick } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
export default defineComponent({
name: 'list',
setup() {
onBeforeRouteLeave((to, from, next) => {
//當即將訪問的界面不是detail則銷毀組件,以免上一次緩存信息存在
const cacheList: any = ['detail']
if (cacheList.indexOf(to.name) === -1) {
//銷毀緩存信息(vue3沒有_this.$destory()方法,所以通過v-if實現(xiàn)組件的銷毀)
//vuex改變全局變量isRouterAlive的值
_this.$store.commit('menu/changeRouterAlive', false)
nextTick(() => {
_this.$store.commit('menu/changeRouterAlive', true)
})
}
next()
})
}
})總結
到此這篇關于vue3.0列表頁面做緩存的文章就介紹到這了,更多相關vue3.0列表頁面緩存內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標的方法
這篇文章主要介紹了vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
詳解在WebStorm中添加Vue.js單文件組件的高亮及語法支持
本篇文章主要介紹了詳解在WebStorm中添加Vue.js單文件組件的高亮及語法支持,非常具有實用價值,需要的朋友可以參考下2017-10-10
vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法
這篇文章主要給大家介紹了關于vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法,記錄一下項目需要注意的地方,方便以后快速使用,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-04-04
基于Vite2.x的Vue 3.x項目的搭建實現(xiàn)
這篇文章主要介紹了基于Vite2.x的Vue 3.x項目的搭建實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04

