Search code examples
javascriptvue.jsvuexstorevue-dynamic-components

Access data inside dynamic components from parent component in VueJS + Vuex


I created this demo to explain better my goal: https://codepen.io/Albvadi/pen/OJMgByR

Each button create a new alert creating a object inside the vuex store in the components array. With the component property I know the type of the component to render.

Each alert generate a random string in a data property inside the component. How do I need to configure the connection with Vuex to obtain the data from the child alert component inside the global components array?

Thanks!


Solution

  • Finally, I solved with a computed property that gives me the parent item. I don't know if it is the best way or if there is a simpler one, but I think it is the correct one.

    computed: Vuex.mapState({
        getParentItem(state) {
            return state.components[state.components.indexOf(this.index)];
        }
    }),
    

    Demo solved: https://codepen.io/Albvadi/pen/wvMrpLv