vue3緩存頁面keep-alive及路由統(tǒng)一處理詳解
一、前言
當(dāng)使用路由跳轉(zhuǎn)到其他頁面的時候,要求緩存當(dāng)前頁面,比如列表頁面跳轉(zhuǎn)到詳情頁面,需要緩存列表內(nèi)容,并且保存滾動條位置,也有時候需要緩存的頁面里面有部分內(nèi)容不緩存,總之各種情況,下面就列舉其中一些例子
vue2和vue3的使用方式是不一樣的
created()方法和mounted()方法在頁面初始化的時候會執(zhí)行,如果緩存了頁面,這兩個方法都不會再執(zhí)行,還有如果緩存了頁面,vue2中的destroyed()和vue3中的unmounted()方法都不會執(zhí)行
activated()方法在每次進(jìn)入頁面都會執(zhí)行
二、使用
1.vue2和vue3的不同
vue2:
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<!-- vue2.x配置 -->
<keep-alive>
<router-view v-if="$route.meta.keepAlive" />
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"/>
</template>
vue3:
<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<!-- vue3.0配置 Component是固定寫法-->
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" v-if="$route.meta.keepAlive"/>
</keep-alive>
<component :is="Component" v-if="!$route.meta.keepAlive"/>
</router-view>
</template>
route.js中配置:
path: '/',
name: 'Home',
component: Home,
meta:{
keepAlive: true
}
效果:

2.頁面某些數(shù)據(jù)不需要緩存
可以在activated()方法中處理需要部分刷新的邏輯
...
需要部分刷新的數(shù)據(jù):<input type="text" v-model="newStr" />
...
data() {
return {
newStr: "2",
};
},
mounted() {
console.log("執(zhí)行了mounted方法");
this.newStr = "3";
},
activated() {
console.log("執(zhí)行了actived方法。。。");
this.newStr = "4";
}
效果:

3.動態(tài)設(shè)置keepAlive屬性
也可以在vue文件中設(shè)置keepAlive屬性,實測只有在beforeRouteEnter()方法中添加才會生效,即進(jìn)入頁面的時候
在Home.vue中添加:
// 使用組件內(nèi)守衛(wèi),對離開頁面事件做一些操作
// to為即將跳轉(zhuǎn)的路由,from為上一個頁面路由
beforeRouteEnter(to, from, next) {
to.meta.keepAlive = true;
// 路由繼續(xù)跳轉(zhuǎn)
next();
}
4.使用include,exclude配置需要緩存的組件
在app.vue中配置:
<router-view v-slot="{ Component }">
<keep-alive include="testKA">
<component :is="Component"/>
</keep-alive>
</router-view>
其中,testKA對應(yīng)某個組件的name:
export default {
name:'testKA',// keep-alive中include屬性匹配組件name
data() {return {}},
activated() {
// keepalive緩存的頁面每次進(jìn)入都會進(jìn)行的生命周期
},
}
此外,include用法還有如下:
<!-- 逗號分隔字符串 --> <keep-alive include="a,b"> <component :is="view"></component> </keep-alive> <!-- 正則表達(dá)式 (使用 `v-bind`) --> <keep-alive :include="/a|b/"> <component :is="view"></component> </keep-alive> <!-- 數(shù)組 (使用 `v-bind`) --> <keep-alive :include="['a', 'b']"> <component :is="view"></component> </keep-alive>
exclude用法與include用法相同,代表不被緩存的組件。
5.部分頁面過來需要緩存,部分頁面過來需要刷新
描述:如有a,b,c三個頁面,a->b時,b刷新頁面,然后b->c,c->b時,b不刷新頁面,再b->a,a->b時,b刷新頁面。
自測,只有配合vuex才能實現(xiàn),但是缺點是,頁面緩存的時候不執(zhí)行activated()方法
6.緩存只在一級路由生效
如果需要在二級路由使用緩存,可以在一級路由中進(jìn)行同樣的配置
store.js代碼:
state: {
keepAlives:[]
},
mutations: {
SET_KEEP_ALIVE(state,params) {
state.keepAlives = params
}
},
getters:{
keepAlive:function(state){
return state.keepAlives
}
}
App.vue代碼:
<template>
<div id="nav">
<router-link to="/bbb">BBB</router-link> |
<router-link to="/home">Home</router-link> |
<router-link to="/about">About</router-link>
</div>
<router-view v-slot="{ Component }">
<keep-alive :include="keepAlive">
<component :is="Component"/>
</keep-alive>
</router-view>
</template>
<script>
export default{
computed:{
keepAlive(){
return this.$store.getters.keepAlive
}
}
}
</script>
Home.vue代碼:
// 使用組件內(nèi)守衛(wèi),對離開頁面事件做一些操作
// to為即將跳轉(zhuǎn)的路由,from為上一個頁面路由
beforeRouteEnter(to, from, next) {
next(vm=>{
if(from.path == "/bbb"){
vm.$store.commit("SET_KEEP_ALIVE",["Home"])
}
});
},
beforeRouteLeave(to, from, next) {
if (to.path == "/about") {
console.log("將要跳轉(zhuǎn)/about頁面...")
} else {
console.log("將要跳轉(zhuǎn)非/about頁面...")
this.$store.commit("SET_KEEP_ALIVE",[])
}
// 路由繼續(xù)跳轉(zhuǎn)
next();
}
效果:

總結(jié)
到此這篇關(guān)于vue3緩存頁面keep-alive及路由統(tǒng)一處理的文章就介紹到這了,更多相關(guān)vue3緩存頁面keep-alive內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue讓router-view默認(rèn)顯示頁面操作方法
一個home頁面,點擊左邊的菜單欄,右邊顯示頁面,因此都知道在右邊放一個router-view然后配置路由即可,然而問題出現(xiàn)在:重新打開的時候,默認(rèn)是白色空的,遇到這樣的問題如何解決呢,下面小編給大家分享Vue讓router-view默認(rèn)顯示頁面操作方法,感興趣的朋友一起看看吧2024-03-03
vue腳手架配置預(yù)渲染及prerender-spa-plugin配置方式
這篇文章主要介紹了vue腳手架配置預(yù)渲染及prerender-spa-plugin配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
vue3配置Element及element-plus/lib/theme-chalk/index.css報錯的解決
這篇文章主要介紹了vue3配置Element及element-plus/lib/theme-chalk/index.css報錯的解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vee-validate vue 2.0自定義表單驗證的實例
今天小編就為大家分享一篇vee-validate vue 2.0自定義表單驗證的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
解決vue中使用Axios調(diào)用接口時出現(xiàn)的ie數(shù)據(jù)處理問題
今天小編就為大家分享一篇解決vue中使用Axios調(diào)用接口時出現(xiàn)的ie數(shù)據(jù)處理問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

