1
votes

After a successful mutation to the vuex store (state.posts.post.comments) using this code, and using Vue.set so Vue can recognize the addition of an object property:

store/modules/post.js

const mutations = {
    [types.SET_POST_COMMENTS] (state, { comments, id }) {
      let post = state.posts.find(post => post._id === id)
      Vue.set(post, 'comments', comments)
    }
}

There is no update to the template or component. The prop post is non-reactive (I assume because even the watcher isn't triggered). I've double checked and the Vuex store for each post's comments property is successfully being updated with a comments object, but the component SinglePost.vue doesn't see these changes.

SinglePost.vue

export default {
  name: 'single-post',
  props: {
    'post': {
      type: Object
    }
  },
  data () {
    return {
      currPost: this.post // tried to reassign post locally
    }
  },
  computed: {
    comments() {
      return this.post.comments // tried to compute comments locally
    }
  },
  watch: {
    post: function(val) { // tried to watch currPost for changes
       console.log('never triggered')
       this.currPost = val 
    }
  }

Ultimately, I can just set a local var by explicitly returning comments from the store to a component method and setting a local comments object, but I want to use my central store (and assume there'd be a way).

SinglePost template

{{comments}} // always empty
{{post}} // doesn't reflect store Vue.set for post.comments
{{currPost}} // doesn't reflect store Vue.set for post.comments

Edit

How I'm getting posts is:

getPosts ({ commit, state, getters, dispatch, rootState }, ctx) {
  //other stuff
  APIHelper.post('/post/search', mergedPayload).then(res => {
    var results = res.data.results
    commit('SET_POSTS', posts || [])
    // where SET_POSTS is just state.posts = posts

the vuex action getPosts is called from Posts.vue component without returning anything since it's set by a mutation @click="getPosts(this.context)" (this works great for setting the posts)

    <div v-for="post in posts">
      <single-post :key="post._id" :post="post" context="feed" />
    </div>
2
Can you show how you're retrieving the post object from the store and passing it to the component as the prop? - thanksd
@thanksd, i updated my post with an "Edit" -- basically posts (contains many post objects) are set on the store, iterated over with v-for in Posts.vue where each post is passed as a prop to SinglePost.vue (i've gotten it to work by retrieving and setting comments in SinglePost, but not using the vuex store) - Kyle
In that v-for, where / how is the data for posts being set? - thanksd
@thanksd, it's set in the SET_POSTS mutation in the vuex store with state.posts = payload called from the getPosts action. The getPosts action is dispatched by the action handler getPosts attached to a click event (@click="getPosts(this.context)") in Posts.vue — in brief, posts is set in the vuex store, not in the component - Kyle
I mean how are you accessing the value of those posts from your component? Do you have a getter set up? Are you using mapGetters? - thanksd

2 Answers

4
votes

You should use vuex's mapGetters helper method.

computed: {
    ...mapGetters({
        currPost: 'GET_CURRENT_POST'
    })
},

It gives access to store state and is reactive so you won't need watchers or additional computeds.

0
votes

Two-way data binding is a great way to achieve this, you can create your own getter/setter method and import it in your Vue components whenever you need it:

export function mapFields(fields)
{
    let computers = {}
    for (let field of fields) {
        computers[field] = {
            get() {
                return this.$store.state[field];
            },
            set(value) {
                this.$store.commit(`UPDATE_${field.toUpperCase()}`, value);
            }
        }
    }
    return computers;
}

Then in your Vue component:

import {mapFields} from 'utils.js'; // or whatever the name of the file

computed: {
   ...mapFields(['mappedState']),
},

Updating this.mappedState in your Vue component:

this.mappedState = ['foo', 'bar'];

will trigger:

this.$store.commit('UPDATE_MAPPEDSTATE', ['foo', 'bar']);

And to get the property's data just call it in your component:

// In your template
{{ mappedState }}

// In Vue methods
this.mappedState;