解決Vue中使用keepAlive不緩存問題
1.查看app.vue文件,這個是重點,不能忘記加(我就是忘記加了keep-alive)
<template>
<div>
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
2.查看router.js
{
path:'/loanmessage',
component:loanmessage,
name:'loanmessage',
meta: {
keepAlive: true, //代表需要緩存
isBack: false,
},
3.在需要緩存的頁面加入如下代碼
beforeRouteEnter(to, from, next) {
if (from.name == 'creditInformation' || from.name == 'cityList') {
to.meta.isBack = true;
}
next();
},
activated() {
this.getData()
this.$route.meta.isBack = false
this.isFirstEnter = false
},
附上鉤子函數(shù)執(zhí)行順序:
- 不使用keep-alive
beforeRouteEnter --> created --> mounted --> destroyed
- 使用keep-alive
beforeRouteEnter --> created --> mounted --> activated --> deactivated
再次進入緩存的頁面,只會觸發(fā)beforeRouteEnter -->activated --> deactivated 。created和mounted不會再執(zhí)行。
總結(jié)
到此這篇關(guān)于Vue中使用keepAlive不緩存問題(已解決)的文章就介紹到這了,更多相關(guān)Vue使用keepAlive不緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue keepAlive 數(shù)據(jù)緩存工具實現(xiàn)返回上一個頁面瀏覽的位置
這篇文章主要介紹了Vue keepAlive 數(shù)據(jù)緩存工具,實現(xiàn)返回上一個頁面瀏覽的位置,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Vue網(wǎng)絡(luò)請求的三種實現(xiàn)方式介紹
這篇文章主要介紹了Vue網(wǎng)絡(luò)請求的三種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-09-09

