Vue组件自定义事件

v-on:xxx=""  :绑定

 this.$emit('xxx')  :  触发

this.$off()   :    解绑

App.vue

<template>
  <div class="app">
    <h1>{{msg}}</h1>
    <!--通过父组件给子组件传递函数类型的props实现:子给父传递参数-->
    <School :getSchoolName="getSchoolName"/>
    <!--通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@或者v-on)-->
    <Student v-on:liner="getStudentName"/>
  </div>
</template>

<script>
import Student from "./components/Student";
import School from "./components/School";
export default {
  name: "App",
  components:{
    Student,
    School
  },
  data(){
    return {
      msg:'你好啊!'
    }
  },
  methods:{
    getSchoolName(name){
      console.log("App收到了学校名",name)
    },
    getStudentName(name,...params){
      console.log("App收到了学生名",name,params)
    }
  }
}
</script>
<style>
  .app{
    background-color: gray;
    padding: 5px;
  }
</style>

Student.vue:通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法,使用@或者v-on), this.$emit触发

<template>
  <div class="student">
    <h2>学生姓名:{{name}}</h2>
    <h2>学生性别:{{sex}}</h2>
    <button @click="sendStudentName">把学生名给app</button>
  </div>
</template>

<script>
export default {
  name: "MyStudent",
  data(){
    return {
      name:'张三',
      sex:'男'
    }
  },
  methods:{
    sendStudentName(){
      //触发Student组件实例身上的liner事件
      this.$emit('liner',this.name,111,222)
    }
  }
}
</script>
<style  scoped>
  .student{
    background-color: orange;
    padding: 5px;
    margin-top: 30px;
  }
</style>

School.vue:通过父组件给子组件传递函数类型的props实现:子给父传递参数

<template>
  <div class="school">
    <h2>学校名称:{{name}}</h2>
    <h2>学校地址:{{address}}</h2>
    <button @click="sendSchoolName">点击提交学校名称</button>

  </div>
</template>

<script>
export default {
  name: "MySchool",
  props:[
      'getSchoolName'
  ],
  data(){
    return {
      name:'山河学aaa',
      address:'山河四省'
    }
  },
  methods:{
    sendSchoolName(){
      this.getSchoolName(this.name)
    }
  }
}
</script>
<style scoped>
  .school{
    background-color: aqua;
    padding: 5px;
  }
</style>

 

 

 解绑:

      // this.$off('liner')//解绑一个自定义事件
      // this.$off(['liner','demo']) //解绑多个自定义事件
         this.$off() //解绑所有的自定义事件

Student.vue

<template>
  <div class="student">
    <h2>学生姓名:{{name}}</h2>
    <h2>学生性别:{{sex}}</h2>
    <button @click="sendStudentName">把学生名给app</button>
    <button @click="unbind">解绑liner组件</button>
  </div>
</template>

<script>
export default {
  name: "MyStudent",
  data(){
    return {
      name:'张三',
      sex:'男'
    }
  },
  methods:{
    sendStudentName(){
      //触发Student组件实例身上的liner事件
      this.$emit('liner',this.name,111,222)
      this.$emit('demo')
    },
    unbind(){
      // this.$off('liner')//解绑一个自定义事件
      // this.$off(['liner','demo']) //解绑多个自定义事件
      this.$off() //解绑所有的自定义事件
    }
  }
}
</script>
<style  scoped>
  .student{
    background-color: orange;
    padding: 5px;
    margin-top: 30px;
  }
</style>

总结:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林代码er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值