【发布时间】:2018-09-12 14:58:46
【问题描述】:
我有一个调用两个函数的简单按钮:在父组件和子组件中:
<btn @click.native="() => {functionParent();
$refs.childComponent.functionChild()}">Call functions<btn>
我知道调用@click 的箭头类型是一种不好的做法,但我稍后会更正它。
现在的问题是functionParent() 需要在functionChild() 开始之前完成。但在我的情况下,functionChild() 在functionParent() 完成之前运行 - 这很糟糕。
我该如何纠正?我想在functionParent() 完成后致电functionChild()。任何想法如何做到这一点?
编辑: (更多细节)
父组件:
<child-comp
:result-array="resultArray"
ref="childComponent">
</child-comp>
<script>
methods: {
functionParent() {
this.resultArray = "result"
}
}
</script>
在子组件中:
<script>
props: ['resultArray'],
methods: {
functionChild() {
if (this.resultArray == "result")
{return "correct"} else {return "done before functionParent()"
}
}
</script>
edit2:我已将this.$refs.childComponent.functionChild() 移动到functionParent() 的末尾,这不会改变结果。似乎子组件中的调用函数是在子组件获取修改后的resultArray
【问题讨论】:
-
@MeetZaveri 我添加了我的代码的更多细节
-
啊哈!很好地检测到这种行为
-
查看这个代码框。看看这个 - codesandbox.io/s/k1oq33qrqv
-
@MeetZaveri 是的,谢谢,现在可以使用了,但谁知道它什么时候会崩溃 :)
-
好的,但是如果几天后有人没有在这里回复,您可以添加您的原始解决方案作为 SO 肯定会接受的唯一答案
标签: javascript vue.js