[Vuejs]-(Vue.js) In computed, I can't use data

4👍

You should not be using arrow functions in the getter and setters, because that refers to the lexical this:

Note that if you use an arrow function with a computed property, this won’t be the component’s instance

So, update your code:

export default {
  data () {
    return {
      test: true
    }
  },
  computed: {
    test2: {
      get: function () {
        return console.log(this.test)
      },
      set: function () {
        console.log(this.test)
      }
  }
}
👤Terry

0👍

this here refers to the function not vue instance ,you can make this:

test2: {
let This = this
      get: () => {
        return console.log(This.test)
      },
      set: () => {
        console.log(This.test)
      }
  }

Leave a comment