[Vuejs]-How can I send data from parent to child component by vuex store in vue component?

3👍

Child’s mounted hook is executed before parent’s mounted hook. ( why? See this link)

console.log(this.getCategory) happens before this.updateCategory(this.category).

Therefore, you get null in the console.

If you put console.log(this.getCategory) in updated hook, you would be getting the right value in the console later on.

2👍

Jacob goh has pointed out the problem.

To solve this issue you can make use of vm.$nextTick() in the child component’s mounted hook to ensure that the entire view has been rendered and the parent’s mounted hook is called.

<template>
    ...
</template>
<script>
    import {mapGetters} from 'vuex'
    ...
    export default {
        ...
        mounted() {
            this.$nextTick(() => {
                console.log(this.getCategory);
            })
        },
        computed: {
            ...mapGetters(['getCategory'])
        },
    }
</script>

Here is the working fiddle

You can learn more about why use vm.nextTick() here: Vue updates the DOM asynchronously

Leave a comment