您好我想弄清楚在Vue应用程序的父级中进行ajax调用的最有效方法是什么,并将其传递给子级,我可以在其中设置所有成功和错误方法。儿童。每个孩子都会以不同的方式处理成功,但我希望孩子知道何时正在进行呼叫,何时完成呼叫,失败等等。最好的方法是什么?
答案 0 :(得分:0)
假设您使用vue-resource
,您的代码可能如下所示
parent.vue
<script>
export default {
methods : {
makeCall () {
var that = this;
// Do something while the API call is in progress.
this.$http.get('/somecall').then(
that.$refs.child.success, // Success
that.$refs.child.failure // Failure
);
}
}
}
</script>
child.vue
<script>
export default {
methods : {
success(res) {
// Do something here
},
failure(res) {
// Do another thing here
}
}
}
</script>
请注意,我自己传递子组件的功能,而不是执行它们。
如果你这样做
console.log(myFunction())
您将获得myFunction
返回的任何值。
如果你这样做
console.log(myFunction)
您将获得一个功能。