美文网首页
Vue中this.$emit()【子组件向父组件传值】的使用

Vue中this.$emit()【子组件向父组件传值】的使用

作者: 兰觅 | 来源:发表于2020-12-24 08:25 被阅读0次

简述

this.$emit()的作用:
子组件向父组件传值
注:必须在父组件中引用子组件,然后实现传值

使用

  • 子组件中
    其中pageVisible为父组件定义函数,flag为需要传递参数
let flag = false;
 this.$emit('pageVisible', flag})
  • 父组件中
    1.引用处添加函数v-on:pageVisible="pageVisible2";
    2.其中pageVisible为子组件中定义函数,
    3.pageVisible2为父组件定义函数——用于接收子组件传值并进行相应数据处理,可定义为同一名称
    4.v-on: 可用 @ 代替 @pageVisible="pageVisible2" ,@ 为 v-on:的简写
<indexImportOrder ref="indexImportOrder" v-on:pageVisible="pageVisible2"/>

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

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

相关文章

网友评论

      本文标题:Vue中this.$emit()【子组件向父组件传值】的使用

      本文链接:https://www.haomeiwen.com/subject/yagynktx.html