$listeners /$attrs

2020-06-19  本文已影响0人  练习时长2年半的个人练习生

使用方法概括:

  <template>
  <div>
    <span>消息:{{message}}</span>
    <Child name="childName1" @send="sendMeassage" age="17岁" />

    <Child name="childName2" @send="sendMeassage" age="18岁" />
  </div>
</template>
<script>
import Child from "../components/child.vue";
export default {
  name: "index",
  components: {
    Child
  },
  data() {
    return {
      message: "初始化"
    };
  },
  methods: {
    sendMeassage(val) {
      this.message = val;
    }
  },
  computed: {}
};
</script>

子组件

<template>
  <div>
    <span @click="send()">{{$attrs}}</span>
   
    
  </div>
</template>
<script>
  export default {
    name: "child",
   methods: {
     send(){
       console.log('sss')
       this.$listeners.send(this.$attrs.age)
     }
   }
  };
</script>
上一篇 下一篇

猜你喜欢

热点阅读