4

My question relates to Vue and more specifically reactivity and reactive getter/setters: https://v2.vuejs.org/v2/guide/reactivity.html

Can I define my own getters in a Vue component and what will happen to them when Vue add its own reactive getters?

8
  • What exactly are you trying to achieve with this? Commented Jun 27, 2018 at 9:44
  • @Stephan-v I come from an angular background and I frequently use getters in my angular components in order to access my component properties without invoking a method. Hence my question. Commented Jun 27, 2018 at 9:47
  • 2
    You can use computed properties for that. They are cached for re-usage and you can add a setter as well. Look for the section of computed setter for a how to here: vuejs.org/v2/guide/computed.html Commented Jun 27, 2018 at 9:49
  • Thanks. I am reading the documentation you pointed me to. Commented Jun 27, 2018 at 9:50
  • 2
    Out of curiosity and quite independently of best practices, what would happen if a developper defined his own user-defined getter in a Vue component? Commented Jun 27, 2018 at 9:52

1 Answer 1

7

Vue will walk through all of its properties and convert them to getter/setters using Object.defineProperty

What the above sentence means is vue iterates over each property in your data option to make them reactive.

For example consider your data option to be:

data:{
  foo: 'hello world',
  bar: 3
}

vue will override the data object as follows(just an abstract description):

let key = Object.keys(data)

for (let i = 0; i < keys.length; i++) {
  let val = data[keys[i]];
  Object.defineProperty(data, keys[i], {
    get(){
      // add this property as a dependency when accessed
      return val;
    },
    set(newVal){
      //notify for a change
      val = newVal;
    }
  })
}

If you check out the vue source code for the same you will find that it checks whether the properties have predefined getters or setters.

Then it overrides the properties getter as follows:

Object.defineProperty(obj, key, {
  enumerable: true,
  configurable: true,
  get: function reactiveGetter() {
    const value = getter ? getter.call(obj) : val;
    if (Dep.target) {
      dep.depend();
      if (childOb) {
        childOb.dep.depend();
        if (Array.isArray(value)) {
          dependArray(value);
        }
      }
    }
    return value;
  },
  set(newVal) {
    //...
  }
});

If you see this line const value = getter ? getter.call(obj) : val; you'll notice that if you defined a getter it is being used and is its value returned.

Vue is just doing some more work to make them reactive by invoking some dependency related methods thats it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for this comprehensive reply! I really appreciate.
I was also looking for some more explanaition. You did a great job. Tnx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.