M
M
Mike2018-04-12 21:03:11
Vue.js
Mike, 2018-04-12 21:03:11

How to make a mutation after deleting an article in vuex?

I get an article, delete it, after me it redirects to the list of all articles. The program works, but only after a redirect, the list of articles is not updated until I refresh the page (F5).
You need to do something with the mutation as I understand it.
mystore.js

... ... ...
const actions = {
    remove_post({ commit }, data) {
        axios.delete('http://localhost:8000/app/posts/' + data + '/')
        .then(response => {
            commit('REMOVE_POST', data)
            console.log('done!')
        })
    }
}
const mutations = {
    REMOVE_POST(state, data) {
        // state.posts = data не понимаю как здесь сделать мутацию
    }
}

my post.vue
import { mapGetters } from 'vuex'
export default {
    name: 'post',
    data() {
        return {
            
            }
    },
    methods: {
        deletePost() {
            this.$store.dispatch('remove_post', this.$route.params.id)
            this.$router.push({name: 'all_posts'})
        }
    },
    created() {
        this.$store.dispatch('get_post', this.$route.params.id)
    },
    computed: mapGetters({
        post: 'getPost'
    }),
    components: {
        Header
    }
                                                           
}
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lemme, 2018-04-12
@google_online

Either I do not understand something, or everything is too simple.
I assume that state.posts is an array of objects (posts), then you need to remove this post from the array, i.e. the banal
ps filter, I suspect that data is the id of the post.

state.posts = state.posts.filter(post => post.id !== data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question