javascript - Vue js从父函数执行子函数并获取数据

标签 javascript vue.js vuejs2 vue-component

我正在构建一个步骤表单,我想执行验证并从我的子组件中获取数据。

所以我有一个步骤形式,它分为容器(父)和步骤1(子)

child 有表单数据

在我的 child

<template>
     thre is the form fiedls
</template>
<script>
 data:()=>({
    orderform:{
      first_name:'',
      lastname:''
    }
  }),

methods:{

 submitOrder(){
  this.$validator.validateAll()
     .then(
      (res)=>{
        if(res){
           //pass this.orderform to parent component

         }   

       }
      )

  }


 }

现在在父级中

<script>
   methods:{

    gotonextStep(){
       //here execute submitOrder function in the child mcomponent
       //am stuck
       //also retrieve order form data here.


    }



    }

</script>

如何在子组件中执行函数并从父组件中检索数据。

母公司结构更新

<stepper>
 <v-stepper-content step="1">
         <child-1></child-1>
    <button  @click.native="gotonextStep">Continue</v-btn>
  </v-stepper-content>
  ...other stepper steps
</stepper>

最佳答案

使用 ref

<child-1 ref="child1"></child-1>

然后在父级中您可以调用方法或从子级获取数据:

methods:{
  goToNextStep(){
     // call child method
     let methodResult = this.$refs.child1.someChildMethod()
     // access data properties in the child
     let childData = this.$refs.child1.someChildDataProperty
  }
}

关于javascript - Vue js从父函数执行子函数并获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46745300/

相关文章:

Vue.js/vuedraggable : Adding "v-model" to a draggable makes it not draggable

authentication - 如何在 vue 组件上使用 auth? (Vue.JS 2)

javascript - 错误率: jquery file upload on submit form button

javascript - 到达方 block 时跳跃算法不起作用

vue-component - Vuex。如何使用 v-for 从 store 渲染数据

javascript - Vuejs $emit 没有在回调时触发父函数

Vue.js 嵌套路由与默认 child

Vue.js:我们应该将脚本或模板放在单个文件组件中以提高生产力吗?

javascript - IE 9 上的 Ajax 调用(成功数据 - 未定义)

javascript - 仅当值有效时,如何通过 Vue 中的 v-model 将更改应用于输入?