2017-02-13 35 views
0

我试图在子组件中做this.updateData()而不是this.$store.dispatch(),其中updateData是一个函数继承自其父组件。任何人有任何想法如何实现这一目标?Vuex中有没有和Redux中的mapDispatchToProps一样的东西?

+0

我想你想要的是将一个道具放置在父组件中绑定到某个方法的子组件中,是吗? – Potray

+0

你能澄清一点我们没有过时的Redux吗?从我推断你的问题你想从子组件触发父功能?我会写下一个实现这个方法的例子。如果我不在那么就忽略我的回答:) – peaceman

回答

0

所以要更新母公司的数据。在Vue公司的子组件,我们可以利用event总线:

家长:

<template> 
    Hi Blake... 
    <child-component @trigger="updateData()"></child-component> 
</template> 
<script> 
    export default { 
    mounted() {}, 
    methods: { 
     updateData: function() { 
     // this will be triggered from child 
     } 
    } 
    } 
</script> 

儿童(儿童成分):

<template> 
    <button @click="$emit('trigger')">Trigger Parent Function</button> 
</template> 
<script> 
    export default { 
    mounted() {} 
    } 
</script> 

现在这只是触发父功能,但你也可以发送数据由父母接收事件。这只是没有Vuex的Vue。如果我错了,而且你不想找到这个,也许你想使用Vuex mapActions,它可以用来导入组件中需要的所有Vuex动作,并将它们用作this.vuexAction而不是this.$store.dispatch('vuexAction')

我希望我能帮上忙。祝你好运。

相关问题