computed的set与get

// computed是一个对象,不是一个函数

computed:{
  fullName () {
    return this.firstName + this.lastName;
  }
}

使用get 与 set

computed:{
  fullName:{  //  这里是一个对象object
   get () { //  每次调用fullName的时候执行
      console.log(this.firstName or lastName is change);
      return this.firstName + lastName;
   },
   set (argu) {  //  vm.fullName = "string1 str2"; 设置computed属性的时候调用
     console.log("调用了fullname的 set function");
     var arr = argu.split(" ");
     this.firstName=arr[0];
     this.lastName = arr[1];
     console.log("这里改变了firstName & lastName get会执行一次,因为是同步任务,");
   }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_38402659/article/details/87873880