Geek-James / ddBuy

🎉Vue全家桶+Vant 搭建大型单页面电商项目.http://ddbuy.7-orange.cn
MIT License
2.44k stars 554 forks source link

使用 async await 简化代码 #19

Open martinwithyou opened 4 years ago

martinwithyou commented 4 years ago
// 数据初始化
_initData () {
  getHomeData().then(response => {
    if (response.success) {
      // 给轮播组件 sowing_list赋值
      this.sowing_list = response.data.list[0].icon_list;
      this.nav_list = response.data.list[2].icon_list;
      this.flash_sale_product_list = response.data.list[3].product_list;
      this.tabbar_all_product_list = response.data.list[12].product_list;
      this.isShowLoading = false
      // 给特色专区赋值
      this.specialZone = response.data.special_zone;
      // 获取首页广告图
      this.home_ad = response.data.home_ad.image_url;
    }
  }).catch(error => {
    console.log(error);
  });
martinwithyou commented 4 years ago

优化点一: 可以使用 async await 简化代码

优化点二: 可以使用 简化 数据结构 const data = response.data 简化代码

// 数据初始化 _initData async () { const response = await getHomeData(); if (response.success) { const data = response.data // 给轮播组件 sowing_list赋值 this.sowing_list = data.list[0].icon_list; this.nav_list = data.list[2].icon_list; this.flash_sale_product_list = data.list[3].product_list; this.tabbar_all_product_list = data.list[12].product_list; this.isShowLoading = false // 给特色专区赋值 this.specialZone = data.special_zone; // 获取首页广告图 this.home_ad = data.home_ad.image_url; } }

Geek-James commented 4 years ago

DuZhu: Right! Thanks a lot give me the code improve suggest.You can send pull requests for me.We maintain together.Thanks you~😁