简体   繁体   English

从 VueJS + Vuex 中的父组件访问动态组件内的数据

[英]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我创建了这个演示来更好地解释我的目标: https://codepen.io/Albvadi/pen/OJMgByR

Each button create a new alert creating a object inside the vuex store in the components array.每个按钮都会创建一个新警报,在组件数组的 vuex 存储中创建一个 object。 With the component property I know the type of the component to render.通过component属性,我知道要渲染的组件的类型。

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?如何配置与 Vuex 的连接以从全局组件数组中的子警报组件获取数据?

Thanks!谢谢!

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演示已解决: https://codepen.io/Albvadi/pen/wvMrpLv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM