尚硅谷-尚品汇项目开发总结(第八天)

13:Object.assign()合并对象

        13.1 是ES6新增的方法,用于对象的拷贝

        13.2 基础使用

<script>
        const x = {a:1,b:2}
        const y = {b:3,c:4}
        const result1 = Object.assign(x,y)
        console.log(result1); // {a:1,b:3,c:4}
        console.log(x);  // {a:1,b:3,c:4}
        console.log(y);  // {b:3,c:4}
    </script>
<script>
        const x = {a:1,b:2}
        const y = {b:{e:7},c:4}
        const result1 = Object.assign(x,y)
        console.log(result1); // {a:1,b:{e:7},c:4}
        console.log(x);  // {a:1,b:{e:7},c:4}
        console.log(y);  // {b:{e:7},c:4}
    </script>
<script>
        const x = {a:1,b:{e:6}}
        const y = {b:{e:7},c:4}
        const result1 = Object.assign(x,y)
        console.log(result1); // {a:1,b:{e:7},c:4}
        console.log(x);  // {a:1,b:{e:7},c:4}
        console.log(y);  // {b:{e:7},c:4}
    </script>

14:mapGetters简化state

        14.1 基础用法

        store中,注意getters中不分模块

// 简化state中的数据,且不分模块
const getters = {
    // 如果获取数据较慢,在还没有获取到数据就执行 getters代码,防止遍历undefined报错
    goodsList:state=>state.searchInfo.goodsList||[]
}

        使用

​
import {mapGetters} from 'vuex'
computed:{
    ...mapGetters(["goodsList"]),
}

​

15:如果服务器的请求参数不是必带的,那么将相应属性置空,仍然会将相应属性带给服务器。如果将相应属性设置为undefined,那么则不会将相应属性带给服务器。

        15.1 将请求参数属性设置为空时

methods:{
    // 删除分类名字
    removeCategoryName(){
      // 将categoryName,category1Id,category2Id,category3Id请求属性置空
      this.searchParams.categoryName = ''
      this.searchParams.category1Id = ''
      this.searchParams.category2Id = ''
      this.searchParams.category3Id = ''
      // 重新获取数据
      this.$store.dispatch("searchInfo",this.searchParams);
    }
  }

        请求数据会发给服务器

         15.2 将请求参数属性设置为undefined时

methods:{
    // 删除分类名字
    removeCategoryName(){
      //
      this.searchParams.categoryName = undefined
      this.searchParams.category1Id = undefined
      this.searchParams.category2Id = undefined
      this.searchParams.category3Id = undefined
      // 重新获取数据
      this.$store.dispatch("searchInfo",this.searchParams);
    }
  }

        请求数据不会发给服务器

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值