Vue: computed variable with arrow functions (second argument on setter)

Created on 22 Feb 2018  路  3Comments  路  Source: vuejs/vue

What problem does this feature solve?

One guy finded, that computed getter may use with arrow function and destructed variables/methods.

I went ahead, and appended this case to set(), but failed, because setter not called component.
Later, i tried use arguments: (locale => arguments[1].a.methods.updateLocale(locale)), but vuex can't use dispatch to this, because undefined.

I think this API looks nice and will reduce many new lines :D

What does the proposed API look like?

export default {
  computed: {
    ...mapGetters(['global']),
    locale: {
      get: ({ global }) => global.locale,
      set: (locale, { updateLocale }) => updateLocale(locale)
    }
  },
  methods: {
    ...mapActions(['updateLocale'])
  }
}

Most helpful comment

I don't believe this is a duplicate of vuejs/vuex#1085. The OP was requesting that a computed setter be passed the component reference as a second argument (which is nothing to do with vuex).

A computed getter is already passed a component reference which allows one to reference component data in an arrow function. But the setter cannot reference component data if defined as an arrow function.

computed: {
  myComputed: {
    get: vm => vm.myData,
    set(value) {
      this.$store.dispatch('updateData', value);
    }
  }
}

If the setter was passed a component reference as the 2nd argument, you could do this:

computed: {
  myComputed: {
    get: vm => vm.myData,
    set: (value, vm) => vm.$store.dispatch('updateData', value)
  }
}

Consider re-opening?

All 3 comments

Duplicate of https://github.com/vuejs/vuex/issues/1085

Make sure to open the issue at the right repo 馃槈

I don't believe this is a duplicate of vuejs/vuex#1085. The OP was requesting that a computed setter be passed the component reference as a second argument (which is nothing to do with vuex).

A computed getter is already passed a component reference which allows one to reference component data in an arrow function. But the setter cannot reference component data if defined as an arrow function.

computed: {
  myComputed: {
    get: vm => vm.myData,
    set(value) {
      this.$store.dispatch('updateData', value);
    }
  }
}

If the setter was passed a component reference as the 2nd argument, you could do this:

computed: {
  myComputed: {
    get: vm => vm.myData,
    set: (value, vm) => vm.$store.dispatch('updateData', value)
  }
}

Consider re-opening?

it was indeed different, opened a properly explained issue with no vuex (which was confusing)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paceband picture paceband  路  3Comments

paulpflug picture paulpflug  路  3Comments

aviggngyv picture aviggngyv  路  3Comments

franciscolourenco picture franciscolourenco  路  3Comments

loki0609 picture loki0609  路  3Comments