這個會一直緩存蔽介,只是判斷是否重新請求接口亮蒋,而不會清空其他緩存的數(shù)據(jù),如果用在列表頁负甸,要清空篩選條件(頁數(shù)流强,搜索框)。
vue緩存頁面(keepAlive,beforeRouterEnter)
需求
從列表頁面進(jìn)入詳情頁呻待,返回列表頁打月,列表頁的數(shù)據(jù)不刷新,存在緩存中
設(shè)置路由
routes:[
{
path: '/list',
name:'list',
component: list,
meta: {
keepAlive: true, // 是否緩存標(biāo)志
title:'列表頁'蚕捉,
isBack: false, // 是否需要刷新
}
},
{
path: '/detail',
name:'detail',
component: detail,
meta:{
title:'詳情頁'
}
},
]
頁面路由嵌套設(shè)置
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
頁面緩存數(shù)據(jù)處理
// 在進(jìn)入頁面時先判斷是否是從詳情頁面返回
beforeRouterEnter (to, from, next) {
if(from.path === '/detail') {
to.meta.isBack = true
}
next()
},
activated () {
// 如果不是從詳情頁面返回奏篙,則重新加載數(shù)據(jù)
if (!this.$route.meta.isBack) {
//加載頁面數(shù)據(jù)
}
// 從新設(shè)置頁面得路由元信息
this.$route.meta.isBack = false
}
1.使用keep-alive進(jìn)行頁面緩存時會生成兩個生命周期:actived,deactivated
2.created:在創(chuàng)建vue對象時,zaihtml標(biāo)簽渲染之前就觸發(fā)迫淹,但是只觸發(fā)一次
3.mounted:是在掛載vue實(shí)例之后秘通,鉤子在掛載時執(zhí)行一次,若頁面沒有緩存敛熬,返回頁面還會再執(zhí)行一次肺稀,若頁面有緩存的話,返回頁面時不再執(zhí)行
4.actived:是組件被激活后的鉤子函數(shù)应民,每次返回頁面都會執(zhí)行