计算属性的setter与getter

计算属性一般没有set方法,只有get方法

绝大多数情况下set都不写

   computed:
      fullName:{
      // set:function ()
      // {
      //
      // },
      get:function ()
      {
        return this.firstName+" "+this.lastName;
      }
      }
    }
  })

一般普遍写法:

      fullName:function ()
      { return this.firstName+" "+this.lastName}
    },

手动设置set属性

      fullName:{
      set:function ()
      {
console.log("....");
      },

1.当更改fullname时,控制台自动输出------

2.给set方法添加参数值newValue,改变fullname时,自动输出----与新的名字

  fullName:{
      set:function (newValue)
      {
console.log("....",newValue);
      },

计算属性的setter与getter_第1张图片

 3.将输入的名字保存起来

  fullName:{
      set:function (newValue)
      {
        console.log("....",newValue);
        const names=newValue.split(" ");
        this.firstName=names[0];
        this.lastName=names[1];
      },

计算属性的setter与getter_第2张图片

 

你可能感兴趣的:(vue学习,javascript,vue.js,html,前端)