0

In my Nuxt application I'm initialising computed properties with data retrieved from the store within the mapState helper, however I also need to modify the value of these computed properties upon certain user actions.

Well I can't do that as I get a "has no setter" error when using that code:

computed: {
    ...mapState({
      monRaw: state => state.currentWeek

I tried to define setters to no avail and the code doesn't make too much sense either:

computed: {
      ...mapState({
            get: function (state) {
              return state.currentWeek
            },
            set: function (state, data) {
              state.currentWeek = data
            }

Is it simply not possible to change computed properties' values when defined with mapState()?

drake035
  • 3,955
  • 41
  • 119
  • 229

1 Answers1

1

mapState is a convenience function that provides a mapping of a computed property under the identical key in your vuex store. It provides read only access to those properties. Since you want to use updates you will have to forego mapstate and define your own computed getter/setter as follows:

computed: {
  currentWeek: {
    get() {
      return this.$store.state.currentWeek;
    },
    set(v) {
      this.$store.commit('updateCurrentWeek', v);
    } 
  }
...
}

obviously updateCurrentWeek will have to be defined in the mutations section of your vuex store.

Andreas Bolz
  • 191
  • 13