[Vuejs]-Is it possible with Vue.js to create a computed property whose value can be changed directly?

0👍

Computed properties are used for returning a calculated (and cached) value, so you cannot directly set value of it because it doesn’t exists at all. Behind computed property are variables that you can set with setter, like in your example. All you need is set new value of referencing variable:

myComputedProperty: {
  get: function () {
    return this.myStoreActionResult;
  },
  set: function (newValue) {
    this.myStoreActionResult = newValue;
    return newValue
  }
}

Using computed variable like in example above is useless, because you indirectly modify original property.

0👍

Yes, it’s possible.

Refer to Vue.js documentation here: Computed Setter

Your code would become something like this:

myComputedProperty: {
  get: function () {
    return this.myStoreActionResult;
  },
  set: function (newValue) {
    this.myStoreActionResult = newVaue;
  }
}

Leave a comment