vue 计算属性 computed

计算属性默认只有getter,所以计算属性的值不能收到设置值,否则会提示没有setter,如果需要可以手动设置setter、

data(){
  
firstName:'',
lastName:''
},
computed:{
  fullName:{
    get:function(){
      return this.firstName+" "+this.lastName
    },
    set:function(newValue){

      let names=newValue.split(" ");
      this.firstName=names[0];
      this.lastName=names[names.length-1];
    }
  }
},
mounted:function(){
  this.
fullName="zhang san";
}

猜你喜欢

转载自www.cnblogs.com/xiaofenguo/p/10402187.html