gpt4 book ai didi

javascript - 是否可以以同步模式执行 $emit 并从 emit 事件中获取结果

转载 作者:搜寻专家 更新时间:2023-10-30 22:56:59 28 4
gpt4 key购买 nike

是否可以同步执行并在调用方法本身时返回结果。所以,我想在 $emit 完成后执行下一条语句。其如下:

Parent component has method, 
doCustomValidation();

child component is as follow:
methods:
{
Save: function(){
// Can I get the response here from parent component and then, execute
doGenericValidation ?

var errorFields = this.$emit('custom-validation');
doGenericValidation(errorFields); //this is generic validations
}

最佳答案

你不应该试图让它同步。相反,您可以使用这个想法:

methods: {
myMethod() {
// Pass a function as a parameter.
this.$emit('custom-validation', this.onComplete);
},
onComplete() {
// Do stuff after custom-validation has completed.
}
}

然后在任何组件中使用您的事件:

<some-component @custom-validation="doStuff" />
<script>
...
methods: {
doStuff(done) {
// Your logic here. After that, call the callback function.
done();
}
}
...

关于javascript - 是否可以以同步模式执行 $emit 并从 emit 事件中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55364122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com