欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實(shí)現(xiàn)頁面緩存功能

 更新時(shí)間:2021年10月28日 08:52:41   作者:無月大大  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁面緩存功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue實(shí)現(xiàn)頁面緩存功能的具體代碼,供大家參考,具體內(nèi)容如下

主要利用keep-alive實(shí)現(xiàn)從列表頁跳轉(zhuǎn)到詳情頁,然后點(diǎn)擊返回時(shí),頁面緩存不用重新請(qǐng)求資源。

一、在router里配置路由

在meta里定義頁面是否需要緩存

import Vue from "vue";
import Router from "vue-router";

// 避免到當(dāng)前位置的冗余導(dǎo)航
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router);
export default new Router({
  base: '',
  routes: [{
      path: "/",
      name: "index",
      component: () => import("@/layout"),
      redirect: '/login',
      children: [
        {
          path: 'dutySheet',
          name: 'dutySheet',
          component: () => import("@/pages/Dashboard/DutySheet")
        },
        {
          path: 'searchWord',
          name: 'searchWord',
          component: () => import("@/pages/dailyReportManage/searchWord/index"),
          meta: {
            keepAlive: true // 需要緩存頁面
          }
        },
        // 匹配維護(hù)
        {
          path: "troopAction",
          name: "troopAction",
          component: () => import("@/pages/Dashboard/TroopAction"),
          meta: {
            keepAlive: false//  不需要緩存
          }
     },
      ]
    },
  ]
});

二、配置APP.vue

使用keep-alive來進(jìn)行緩存

<keep-alive>
    <router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>

三、點(diǎn)擊返回按鈕時(shí)調(diào)用this.$router.back()方法就可以了

// 返回
      bacKBnt(){
        this.$router.back()
      },

四、清除緩存

只針對(duì)跳轉(zhuǎn)到"exhibitionWord"或"exhibitionWeekWord"頁面才進(jìn)行緩存,跳轉(zhuǎn)其他頁面不用緩存。

beforeRouteLeave(to, from, next) {
      if (to.name == 'exhibitionWord' || to.name == 'exhibitionWeekWord') { // 需要緩存的路由name
          from.meta.keepAlive = true
          next()
        }else{
          from.meta.keepAlive = false
          next()
      }
    },

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論