this.$emit()方法 Vue子组件向父组件传值

子组件使用this.$emit()向父组件传值

  • 第一步 在父组件中引入子组件


<indexImportOrder ref="indexImportOrder" @closeMain="closeMain"/>


import indexImportOrder from './components/indexImportOrder'

components:{
        indexImportOrder,
      }
  • 第二步 子组件向父组件传值

1.  使用this.$emit("function",param);   //其中function为父组件定义函数,param为需要传递参数


//新订单页面跳转
        viewBusiness(){
          let flag = false;
          this.$emit('closeMain',flag);
        },

2.  在父组件中子组件引用处添加函数@:function="function1"; //其中function为子组件中定义函数,

//function1为父组件定义函数--用于接收子组件传值并进行相应数据处理,可定义为同一名称

<indexImportOrder ref="indexImportOrder" v-on:closeMain="closeMain"/>

val及为子组件中flag,即接收的子组件参数


closeMain(val){
        this.flag = val;
      },

猜你喜欢

转载自blog.csdn.net/weixin_37380784/article/details/89707688