vue parent成分从子方法获取响应

发布于 2025-02-09 04:57:31 字数 488 浏览 0 评论 0原文

我有父母组成部分和一个孩子。 从父母那里,我想调用儿童组件中的方法并等待响应。我该怎么做? 我需要响应以在父方法中添加条件。

<!-- MY CHILD COMPONENT -->
<my-child-component ref="childform" />

// parent method
methods: {
     callChildMethod() {
        this.$refs.childform.onSubmit()
        // if want to get the response and do something
        if (response) {
            //do something
        }
      },
}

//child method
methods: {
    onSubmit() {
      //do something
      return false
    },
}

I have a parent component and a child.
From parent i want to call a method in child component and wait the response. How can i do that?
I need the response for adding a condition in the parent method.

<!-- MY CHILD COMPONENT -->
<my-child-component ref="childform" />

// parent method
methods: {
     callChildMethod() {
        this.$refs.childform.onSubmit()
        // if want to get the response and do something
        if (response) {
            //do something
        }
      },
}

//child method
methods: {
    onSubmit() {
      //do something
      return false
    },
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2025-02-16 04:57:31

您的问题可以使用参考来解决,并且可以如下工作:
在父组件中:

 <parent @click="clickHandler">
    <child ref="child" />
 </parent>

在您的父脚本标签中:

methods: {
   async clickHandler(){
      //call child component's method using reference given in parent component
         await this.$refs.child.child_method_name();
      // rest of your parent code here
   }
}

Your problem could be solved using refs and it works as follows:
In parent component:

 <parent @click="clickHandler">
    <child ref="child" />
 </parent>

In your parent script tag:

methods: {
   async clickHandler(){
      //call child component's method using reference given in parent component
         await this.$refs.child.child_method_name();
      // rest of your parent code here
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文