计算属性默认只有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]
    }
  }
}
// ...

在运行vm.fullName='John Doe'时,setter会被调用,vm.firstName和vm.lastName也会相应更新。

results matching ""

    No results matching ""