Vue keepAlive頁面強制刷新方式
keepAlive頁面強制刷新
需求
現(xiàn)在有一個需求,要求不刷新瀏覽器,但要刷新路由中的組件
方案
將需要keepAlive的頁面name加入到keep-live的include中。
Vue的transition組件,有一個after-enter鉤子事件,待子組件插入完畢調用,正好適合這個場景,在這個鉤子中,將當前組件添加到keep-live的include中。
在刷新時,從keepAliveArr中移除當前組件的name,然后利用v-if刪除router-view組件,在nextTick事件后將router-view添加回來,實現(xiàn)組件的刷新
代碼
template
<transition @after-enter="afterRouterChange"> ? <keep-alive :include="keepAliveArr"> ? ? <router-view v-if="refreshControl" ref="child"/> ? </keep-alive> </transition> <button @click="refreshChild"></button>
script
export default {
? name: 'index',
? data () {
? ? return {
? ? ? keepAliveArr: [],
? ? ? refreshControl: true
? ? }
? },
? methods: {
? ? refreshChild () {
? ? ? // 先移除,再加載,強制刷新子頁面
? ? ? const name = this.$refs.child.$options.name
? ? ? this.keepAliveArr.splice(this.keepAliveArr.indexOf(name), 1)
? ? ? this.refreshControl = false
? ? ? this.$nextTick(() => this.refreshControl = true)
? ? },
? ? afterRouterChange () {
? ? ? // 記錄子組件name,用于keepalive
? ? ? const childName = this.$refs.child.$options.name
? ? ? this.pageTabList[this.pageTabIndex].name = childName
? ? ? if (!this.keepAliveArr.includes(childName)) {
? ? ? ? this.keepAliveArr.push(childName)
? ? ? }
? ? }
? }
}keep-alive緩存頁面刷新
vue文件keep-alive緩存頁面刷新
概述:
vue開發(fā)項目時,需要緩存多個頁面的情況
使用場景:
例如:現(xiàn)有4個頁面,頁面1,頁面2,頁面3,頁面4
要求:
- 1、從1-2-3-4依次跳轉時,每次都刷新頁面,不進行緩存;
- 2、從4-3-2-1依次返回時,頁面不刷新,依次取緩存頁面;
總結:外到內都需要刷新,內到外皆獲取緩存頁面;
實現(xiàn)方式:keep-alive、vuex、路由鉤子函數(shù)beforeRouteEnter、beforeRouteLeave
1.vuex部分
import Vue from 'vue';
import Vuex from 'vuex';
let storeModules = {};
Vue.use(Vuex);
export default new Vuex.Store({
? ? state: {
? ? ? ? keepAlive: []
? ? },
? ? getters: {},
? ? mutations: {
? ? ? ? change_keepalive: (state, keepAlive) => {
? ? ? ? ? ? state.keepAlive = keepAlive;
? ? ? ? }
? ? },
? ? actions: {},
? ? modules: storeModules
});2.app.vue部分
<template>
? ? <div>
? ? ? ? <keep-alive :include="$store.state.keepAlive">
? ? ? ? ? ? <router-view></router-view>
? ? ? ? </keep-alive>
? ? </div>
</template>
<script>
export default {};
</script>使用<keep-alive>的include屬性,來實現(xiàn)動態(tài)的組件緩存。
先說一下include屬性,它的值可以是:字符串,正則表達式,數(shù)組
首先我們需要知道keep-alive可以根據(jù)include中的值來匹配當前路由對應組件的name屬性,來判斷這個路由中的組件是否需要緩存。
3.頁面1內部寫法
methods: {
? ? ?// 跳轉
? ? ?goLink(){
? ? ? ? ?this.$store.commit('change_keepalive', ['頁面1','頁面2','頁面3'])?
? ? ? ? ?this.$router.push({
? ? ? ? ? ? ?path:'/頁面2',
? ? ? ? ?})
? ? ?}
?},
?beforeRouteEnter (to, from, next) {
? ? next(vm => {
? ? ? vm.$store.commit('change_keepalive', ['頁面1'])
? ? })
? }4.頁面2內部寫法
beforeRouteEnter (to, from, next) {
? ? next(vm => {
? ? ? if (from.path.indexOf('頁面3') > -1) {
? ? ? ? ? vm.$store.commit('change_keepalive', ['頁面1','頁面2'])
? ? ? }
? ? })
? },
? beforeRouteLeave (to, from, next) {
? ? if (to.path.indexOf('頁面3') > -1) {
? ? ? this.$store.commit('change_keepalive', ['頁面1','頁面2', '頁面3'])
? ? } else if (to.path.indexOf('頁面1')>-1) {
? ? ? this.$store.commit('change_keepalive', ['頁面1'])
? ? }
? ? next()
? }5.頁面3內部寫法
beforeRouteEnter (to, from, next) {
? ? next(vm => {
? ? ? if (from.path.indexOf('頁面4') > -1) {
? ? ? ? ? vm.$store.commit('change_keepalive', ['頁面1','頁面2', '頁面3'])
? ? ? }
? ? })
? },
? beforeRouteLeave (to, from, next) {
? ? if (to.path.indexOf('頁面4') > -1) {
? ? ? this.$store.commit('change_keepalive', ['頁面1','頁面2', '頁面3'])
? ? } else if (to.path.indexOf('頁面2') > -1) {
? ? ? this.$store.commit('change_keepalive', ['頁面1','頁面2'])
? ? }
? ? next()
? }6.頁面4
不需要緩存則無需添加任何東西,正常書寫即可,如需添加設置緩存,則按照上方更改添加配置即可。
注:
其中from.path.indexOf('頁面x')中頁面x為router的path屬性
this.$store.commit('change_keepalive', ['頁面1','頁面2'])中的頁面x為組件的name屬性(不是路由的name,是組件的name)
<script>
? ? export default {
? ? ? ? name:'index',//組件name
? ? ? ? components: {},
? ? ? ? props: {},
? ? ? ? data() {
? ? ? ? ? ? return {};
? ? ? ? },
? ? ? ? computed: {},
? ? ? ? watch: {},
? ? ? ? created() {},
? ? ? ? mounted() {},
? ? ? ? methods: {}
? ? }
</script>```總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue基礎之data存儲數(shù)據(jù)及v-for循環(huán)用法示例
這篇文章主要介紹了vue基礎之data存儲數(shù)據(jù)及v-for循環(huán)用法,結合實例形式分析了vue.js使用data存儲數(shù)據(jù)、讀取數(shù)據(jù)及v-for遍歷數(shù)據(jù)相關操作技巧,需要的朋友可以參考下2019-03-03
vue+element實現(xiàn)下拉菜單并帶本地搜索功能示例詳解
這篇文章主要介紹了vue+element實現(xiàn)下拉菜單并帶本地搜索功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
Vue3結合SpringBoot打造一個高效Web實時消息推送系統(tǒng)
這篇文章主要為大家詳細介紹了Vue3如何結合SpringBoot打造一個高效Web實時消息推送系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-07-07
Vue?props傳入function時的this指向問題解讀
這篇文章主要介紹了Vue?props傳入function時的this指向問題解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

