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

vue keep-alive請求數(shù)據(jù)的方法示例

 更新時(shí)間:2018年05月16日 09:20:14   作者:37度檸檬水  
本篇文章主要介紹了vue keep-alive請求數(shù)據(jù)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

背景

index頁面:首頁品牌入口

list頁面:商品列表頁面

product頁面:商品詳情頁面

從index頁面進(jìn)入list的時(shí)候要刷新頁面,從product頁面返回list的時(shí)候不需要刷新頁面,所以list使用了keep-alive的屬性,keepAlive設(shè)置為true,但是開發(fā)過程中發(fā)現(xiàn)一個(gè)問題,從product返回到list的時(shí)候,列表頁面不是正確的品牌列表,而是之前一次點(diǎn)擊的品牌列表。由于每個(gè)人遇到關(guān)于keep-alive請求數(shù)據(jù)不正確的問題不同,這里就直接說我的解決辦法。希望能給大家一個(gè)思路。

解決辦法

很多人都會(huì)通過修改keepAlive來改變keep-alive,我嘗試后還是不行,就換了個(gè)思路

鉤子函數(shù)的執(zhí)行順序

不使用keep-alive

beforeRouteEnter --> created --> mounted --> destroyed

使用keep-alive

beforeRouteEnter --> created --> mounted --> activated --> deactivated

先掃盲,多少人和我都不知道上面的知識(shí),在keep-alive的頁面中,可以在 activated獲取this.$route.params的參數(shù)

避開了設(shè)置keepAlive導(dǎo)致product返回的時(shí)候數(shù)據(jù)不對(duì),當(dāng)頁面進(jìn)入list的時(shí)候都是緩存狀態(tài),然后再通過是不是由index進(jìn)入來判斷是否執(zhí)行activated里的函數(shù),

list.vue 

   beforeRouteEnter(to, from, next) {
   //判斷從index頁面進(jìn)入,將list的isBack設(shè)置為true
   //這樣就可以請求數(shù)據(jù)了
     if (from.name == 'index') {
      to.meta.isBack = true;
     }
     next();
   },
   activated: function () {
     if (this.$route.meta.isBack || this.isFirstEnter) {
      //清理已有商品列表的數(shù)據(jù),重新請求數(shù)據(jù),如果不清除的話就會(huì)有之前的商品緩存,延遲顯示最新的商品
      this.proData = [];
      //請求數(shù)據(jù)
      this.fetchData();
     }
     //重新設(shè)置當(dāng)前路由的isBack
     this.$route.meta.isBack = false;
     //重新設(shè)置是否第一次進(jìn)入
     this.isFirstEnter = false;
   },
   mounted: function () {
    //如果是第一次進(jìn)入,或者刷新操作的話,也請求數(shù)據(jù)
     this.isFirstEnter = true;
   },

router.js

const appRouter = {
 mode: "history",
 base: "/m/",
 routes: [
  {
   path: "",
   redirect: "/index"
  },
  {
   path: "/index",
   name: "index",
   component: Index,
   meta: {
    keepAlive: true
   }
  },
    {
   path: "/list",
   name: "list",
   component: List,
   meta: {
    keepAlive: true,
    isBack: false //isback是true的時(shí)候請求數(shù)據(jù),或者第一次進(jìn)入的時(shí)候請求數(shù)據(jù)
   }
  },
  {
   path: "/product/:id",
   name: "product",
   component: Product,
   meta: {
    keepAlive: false
   }
  }
  
 ]
};

Vue.use(Router);
export default new Router(appRouter);

不知道有咩有幫大家理清思路,有問題可以留言,以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

太康县| 凭祥市| 无极县| 修水县| 江达县| 峨眉山市| 澄城县| 远安县| 朔州市| 乐安县| 曲水县| 兴仁县| 绥德县| 彰化县| 外汇| 太谷县| 麻阳| 银川市| 盱眙县| 林芝县| 阳谷县| 昔阳县| 夏河县| 永川市| 新昌县| 托克逊县| 舟曲县| 安吉县| 武清区| 平阳县| 高邑县| 常州市| 正安县| 家居| 泰来县| 盐亭县| 花莲县| 界首市| 漳州市| 惠安县| 宿松县|