最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue keepAlive頁面強制刷新方式

 更新時間:2023年05月24日 15:41:13   作者:milugloomy  
這篇文章主要介紹了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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • vuex與組件聯(lián)合使用的方法

    vuex與組件聯(lián)合使用的方法

    Vuex 是一個專為 Vue.js 應用程序開發(fā)的狀態(tài)管理模式。這篇文章主要介紹了vuex與組件聯(lián)合使用的方法,需要的朋友可以參考下
    2018-05-05
  • vue基礎之data存儲數(shù)據(jù)及v-for循環(huán)用法示例

    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)下拉菜單并帶本地搜索功能示例詳解

    這篇文章主要介紹了vue+element實現(xiàn)下拉菜單并帶本地搜索功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Vue3結合SpringBoot打造一個高效Web實時消息推送系統(tǒng)

    Vue3結合SpringBoot打造一個高效Web實時消息推送系統(tǒng)

    這篇文章主要為大家詳細介紹了Vue3如何結合SpringBoot打造一個高效Web實時消息推送系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以了解下
    2025-07-07
  • 對vue中v-if的常見使用方法詳解

    對vue中v-if的常見使用方法詳解

    今天小編就為大家分享一篇對vue中v-if的常見使用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue項目如何部署到SpringBoot工程下

    Vue項目如何部署到SpringBoot工程下

    這篇文章主要介紹了Vue項目如何部署到SpringBoot工程下問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vue3新特性與最佳實踐總結與開發(fā)技巧

    Vue3新特性與最佳實踐總結與開發(fā)技巧

    在Vue3中性能優(yōu)化是提升用戶體驗的關鍵,通過合理地利用Vue3的新特性和優(yōu)化策略,可以顯著提升應用的加載速度和運行效率,這篇文章主要介紹了Vue3新特性與最佳實踐總結與開發(fā)技巧的相關資料,需要的朋友可以參考下
    2025-11-11
  • Vue?props傳入function時的this指向問題解讀

    Vue?props傳入function時的this指向問題解讀

    這篇文章主要介紹了Vue?props傳入function時的this指向問題解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue3中defineModel用法及常見問題完全指南

    Vue3中defineModel用法及常見問題完全指南

    defineModel是Vue 3.3 引入、3.4 穩(wěn)定的一個編譯器宏,用于簡化組件的雙向數(shù)據(jù)綁定實現(xiàn),它讓開發(fā)者能夠更輕松地創(chuàng)建支持v-model的組件,減少了樣板代碼,提高了開發(fā)效率,這篇文章主要介紹了Vue3中defineModel用法及常見問題的相關資料,需要的朋友可以參考下
    2026-02-02
  • Vue列表如何實現(xiàn)滾動到指定位置樣式改變效果

    Vue列表如何實現(xiàn)滾動到指定位置樣式改變效果

    這篇文章主要介紹了Vue列表實現(xiàn)滾動到指定位置樣式改變效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05

最新評論

阳山县| 莱西市| 广河县| 将乐县| 三原县| 澎湖县| 云南省| 台东县| 陇西县| 哈巴河县| 中西区| 永济市| 清镇市| 南溪县| 北流市| 米易县| 宁明县| 义马市| 道孚县| 金乡县| 信丰县| 阿荣旗| 略阳县| 紫金县| 友谊县| 景谷| 宜兰县| 琼结县| 长葛市| 土默特左旗| 江永县| 永定县| 兴和县| 易门县| 平阳县| 昌吉市| 连平县| 故城县| 巩义市| 类乌齐县| 兴山县|