N
N
nurdus2018-07-03 23:40:37
Vue.js
nurdus, 2018-07-03 23:40:37

Why might a func type field be lost in vuex?

Good evening.
There is a storage and the filters field in it:

//...
const createStore = () => {
  return new Vuex.Store({
    state: {
      jobs: [],
      filters: {}
    },
    //...
    actions: {
      async nuxtServerInit({ commit }) {
        const result = await api.getJobs()
        commit("SET_JOBS", result.data)
        commit("SET_FILTERS", filters)
      }
    }
  })
}

which is filled in from the filters.js file (in the future it will be json pulled through axios):
export default {
  incoming: {
    approving: {
      title: "Согласование",
      filterFunc: function(iJob) {
        return iJob.subject.match(/Согласуйте.*/)
      }
    }
    //...
  }
}

and the component itself, if you also import filters.js in it and work with it (not from the storage), then everything works, BUT if you access filters from the storage, then the object loses filterFunc:
<template>
  <div>
    <!--тут всё ок, в хранилище оно есть-->
    <h5>{{ this.$store.state.filters.incoming[$route.params.filter].title }}</h5>
    <JobList :jobs = filteredJobs />
  </div>
</template>

<script>
//...
export default {
  data() {
    return {
      filters
    }
  },
  computed: mapState({
    filteredJobs: function(state) {
      const filterName = this.$route.params.filter
      //const filterFunc = this.filters.incoming[filterName].filterFunc // так РАБОТАЕТ
      const filterFunc = state.filters.incoming[filterName].filterFunc  // так НЕ работает
      return jobs
    }
  }),
  //...
}
</script>

and the strangest thing is that with ssr (that is, if you open the page and refresh), then it is first filled in, and then reset with the comment "undefined is not a function". Of course, you can leave it as it is, but I would like to understand what is wrong ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Koteezy, 2018-07-04
@nurdus

If you use Nuxt, then for some reason it does not allow methods in the repository. :(
https://github.com/nuxt/nuxt.js/issues/3227

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question