JS ES6 rest参数回调小技巧

拓展运算符(...)用于取出参数对象所有可遍历属性然后拷贝到当前对象

//js对象中
let b ={c:100,d:100}
let a ={
      x:100,
      y:100,
      ...b      //相当于c:100,d:100          
}  

  

在 Vue组件method中定义了方法调用回调函数传参时,如果传参需要指定参数和多个参数时。可以

  methods:{         
            sendStudentName(){
                   this.$emit('weiwei',this.name,666,888,900)
            }
        }
  getStudentName(name,...params){
      // alert('111') 
      alert('APP已收到!'+name+params)//name和参数数组。
    }

 在更新一下一个新的技巧,当自己定义一个对象里面的数据和回调的对象中属性不匹配,但是没有用this而是直接替换对象

//自己的数据对象info
 data() {
        return {
          info:{
            users:[],
            isLoading:false,
            isFirst:true,
            errMsg:''
          } 
        }
  }
//传递回来的数据没有isFirst属性,直接替换了原对象
//在更新数据是用rest参数两者可以进行合并,回调对象中没有自身属性的话会使用自身的属性而不是全部替换 比挨着每一项遍历this要方便
 mounted() {
        this.$bus.$on('updateUsers',dataObj=>{
             this.info = {...this.info,...dataObj}
        })
    },
 

这样可以使info对象中仍然带着isFirst属性,相当于两对象合并。

posted @ 2021-10-11 21:08  Frose  阅读(236)  评论(0)    收藏  举报