VUE学习(一):$emit 和 $on

271 阅读1分钟

on:

  • 用法

    1. 一个事件定义多个执行函数

    this.$on("msg", this.handle)

    this.$on("msg", this.handle)

    2. 可以定义多个事件

    this.$on(["msg1","mag2"], this.handle)

  • 原理

emit:

  • 用法

    doEvent(){ this.$emit("msg", this.a) }

<template>
  <div class="wrap">
      <button @click="doEvent">dfdsfsd</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
        a: '123'
    };
  },
  mounted() {
     
    this.$on("msg", this.handle)
  },
  methods:{
      handle(e){
          console.log("111")
          console.log(e)
      },
      doEvent(){
          this.$emit("msg", this.a)
      }
  }
};
</script>