计算属性 vs 侦听属性

计算属性默认只有 getter,不过在需要时你也可以提供一个 setter:

/ ...
computed: {
  fullName: {
    // getter
    get: function () {
      return this.firstName + ' ' + this.lastName
    },
    // setter
    set: function (newValue) {
      var names = newValue.split(' ')
      this.firstName = names[0]
      this.lastName = names[names.length - 1]
    }
  }
}
// ...

 

posted on 2022-03-07 15:32  zhishiyv  阅读(17)  评论(0编辑  收藏  举报

导航