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

前端 未结 2 1382
醉酒成梦
醉酒成梦 2020-12-22 13:22

My parent component like this :




        
相关标签:
2条回答
  • 2020-12-22 14:02

    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

    0 讨论(0)
  • 2020-12-22 14:17

    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.

    0 讨论(0)
提交回复
热议问题