vue2.0 中#$emit,$on的使用详解

作者:m0_37068028 时间:2023-07-02 16:52:27 

vue1.0中 vm.$dispatch 和 vm.$broadcast 被弃用,改用$emit,$on


vm.$on( event, callback )

监听当前实例上的自定义事件。事件可以由vm.$emit触发。回调函数会接收所有传入事件触发函数的额外参数。


vm.$emit( event, […args] )

触发当前实例上的事件。附加参数都会传给 * 回调。

例子:


//父组件
<template>
 <ratingselect @select-type="onSelectType"></ratingselect>
</template>
<script>
 data () {
  return {
   selectType: 0,
 },
 methods: {
  onSelectType (type) {
   this.selectType = type
  }
 }
</script>

父组件使用@select-type="onSelectType"@就是v-on的简写,监听由子组件vm.$emit触发的事件,通过onSelectType()接受从子组件传递过来的数据,通知父组件数据改变了。


// 子组件
<template>
<div>
 <span @click="select(0, $event)" :class="{'active': selectType===0}"></span>
 <span @click="select(1, $event)" :class="{'active': selectType===1}"></span>
 <span @click="select(2, $event)" :class="{'active': selectType===2}"></span>
</div>
</template>
<script>
 data () {
  return {
   selectType: 0,
 },
 methods: {
   select (type, event) {
     this.selectType = type
     this.$emit('select-type', type)
  }
 }
</script>

子组件通过$emit来触发事件,将参数传递出去。

以上所述是小编给大家介绍的vue2.0 中#$emit,$on的使用详解网站的支持!

来源:http://blog.csdn.net/m0_37068028/article/details/72898119

标签:vue2.0,on,emit
0
投稿

猜你喜欢

  • Python pytorch实现绘制一维热力图

    2022-04-03 21:09:14
  • Python生成器常见问题及解决方案

    2023-01-23 19:09:43
  • asp如何编写翻页函数?

    2009-11-07 18:46:00
  • 简单了解python反射机制的一些知识

    2022-02-05 15:01:04
  • MySQL存储过程中游标循环的跳出和继续操作示例

    2024-01-25 05:32:04
  • 浅谈Python NLP入门教程

    2021-04-18 14:45:53
  • 浅析Python中的序列化存储的方法

    2022-09-28 19:21:26
  • python实现学生管理系统源码

    2023-05-29 22:05:16
  • Python定时任务工具之APScheduler使用方式

    2022-02-02 05:50:51
  • goland -sync/atomic原子操作小结

    2024-04-26 17:20:08
  • 基于python 的Pygame最小开发框架

    2022-01-23 12:22:40
  • ASP状态封装类Cache、Cookie & Session

    2008-05-11 19:33:00
  • 浅析python中5个带key的内置函数

    2021-08-27 00:31:25
  • Python中模块与包有相同名字的处理方法

    2021-11-16 07:58:34
  • Python使用matplotlib简单绘图示例

    2023-11-03 01:33:00
  • 火车票抢票python代码公开揭秘!

    2021-06-16 18:34:29
  • 详解pandas库pd.read_excel操作读取excel文件参数整理与实例

    2021-08-07 00:06:22
  • SQL server使用自定义函数以及游标

    2011-11-03 17:26:27
  • PyTorch实现手写数字的识别入门小白教程

    2021-02-04 19:58:59
  • XHTML1.0规范:您是否为img图片标签赋予alt属性

    2009-09-21 11:11:00
  • asp之家 网络编程 m.aspxhome.com