关于vue.js:后端小伙伴来学前端了Vue中组件绑定自定义事件实现通信

5次阅读

共计 3667 个字符,预计需要花费 10 分钟才能阅读完成。

黄昏的月亮

前言

本来这篇打算写 Vue 中的那个全局事件总线的原理,然而发现自己少写了这个自定义事件,不讲明确这个自定义事件的操作,不好写全局事件原理,于是就有了这篇文章拉。

一、v-on 指令

要讲自定义事件,就得先说说 v -on 指令。因为 v-on 就是实现自定义事件的根底。

v-on 官网文档

根本介绍

v-on指令能够缩写为 @,并且咱们应用v-on 指令时,其实它有一个默认参数event.

能够和它一起搭配的修饰符大抵有以下几种:

  1. .stop – 调用 event.stopPropagation()。进行冒泡
  2. .prevent – 调用 event.preventDefault()。阻止默认行为
  3. .capture – 增加事件侦听器时应用 capture 模式。
  4. .self – 只当事件是从侦听器绑定的元素自身触发时才触发回调。
  5. .{keyCode | keyAlias} – 只当事件是从特定键触发时才触发回调。键修饰符,键别名
  6. .native – 监听组件根元素的原生事件。
  7. .once – 只触发一次回调。
  8. .left – (2.2.0) 只当点击鼠标左键时触发。
  9. .right – (2.2.0) 只当点击鼠标右键时触发。
  10. .middle – (2.2.0) 只当点击鼠标中键时触发。
  11. .passive – (2.3.0) 以 {passive: true} 模式增加侦听器

这些修饰符局部是能够串联起来应用的。

作用:

  • 绑定事件监听器。事件类型由参数指定。表达式能够是一个办法的名字或一个内联语句,如果没有修饰符也能够省略。
  • 用在一般元素上时,只能监听 原生 DOM 事件 用在自定义元素组件上时,也能够监听子组件触发的自定义事件。

    明天第二点才是咱们滴重点哈。

示例:

<!-- 办法处理器 -->
<button v-on:click="doThis"></button>

<!-- 动静事件 (2.6.0+) -->
<button v-on:[event]="doThis"></button>

<!-- 内联语句 -->
<button v-on:click="doThat('hello', $event)"></button>

<!-- 缩写 -->
<button @click="doThis"></button>

<!-- 动静事件缩写 (2.6.0+) -->
<button @[event]="doThis"></button>

<!-- 进行冒泡 -->
<button @click.stop="doThis"></button>

<!-- 阻止默认行为 -->
<button @click.prevent="doThis"></button>

<!-- 阻止默认行为,没有表达式 -->
<form @submit.prevent></form>

<!--  串联修饰符 -->
<button @click.stop.prevent="doThis"></button>

<!-- 键修饰符,键别名 -->
<input @keyup.enter="onEnter">

<!-- 键修饰符,键代码 -->
<input @keyup.13="onEnter">

<!-- 点击回调只会触发一次 -->
<button v-on:click.once="doThis"></button>

<!-- 对象语法 (2.4.0+) -->
<button v-on="{mousedown: doThis, mouseup: doThat}"></button>

在子组件上监听自定义事件 (当子组件触发“my-event”时将调用事件处理器):

<my-component @my-event="handleThis"></my-component>

<!-- 内联语句 -->
<my-component @my-event="handleThis(123, $event)"></my-component>

<!-- 组件中的原生事件 -->
<my-component @click.native="onClick"></my-component>

看了这个 v-on 之后,不晓得大家有没有想起 VueComponent 实例上的$on,即vm.$on(event,callback)

  • vm.$on(event,callback)用法

    监听以后实例上的自定义事件。事件能够由 vm.$emit 触发。回调函数会接管所有传入事件触发函数的额定参数。

在此处,倡议大家思考一下他们的区别,因为 vm.$on 其实就是实现全局事件总线的原理。

二、自定义事件

简略图示:

咱们给在 App 组件中,通过 v -on 或者 @给 A 组件绑定一个自定义事件,它的触发机会是等到 A 组件在外部调用 this.$emit(’myevent‘), 之后就会触发 App 组件中的回调。

实际上咱们给 A 组件通过 v-on 绑定一个 自定义事件 ,其本质就是咱们在 A 组件实例对象VC 上绑定了一个事件,事件名字叫咱们自定义的名称。

因为咱们写了一个 <A></A> 组件标签,Vue 底层也是要帮咱们 new VueComponent()对象。

对于自定义事件名

自定义事件名它不同于组件和 prop,事件名不存在任何自动化的大小写转换。只有事件名称齐全匹配时能力监听这个事件。

v-on事件监听器在 DOM 模板中会被主动转换为全小写,所以 v-on:myEvent 将会变成 v-on:my-event 从而导致 myEvent不可能被监听到。

vue 始终举荐你始终应用 kebab-case 的事件名。

三、入门案例

实现成果

App 组件

<template>
  <div id="app">
    <!-- props 办法传递 -->
    <Demo :showMsg="showMsg"></Demo>
    <!-- 绑定自定义事件 send-message: 是咱们自定义事件名,前面的 sendMessage 自定义事件被触发执行的回调函数 -->
    <Demo1 v-on:send-message="sendMessage"></Demo1>
  </div>
</template>

<script>
import Demo from "./components/Demo.vue";
import Demo1 from "./components/Demo1.vue";

export default {
  name: "App",
  components: {
    Demo,
    Demo1,
  },
  methods: {showMsg() {alert("收到来自 demo 的信息");
    },
    sendMessage(value) {alert("sendMessage 自定义事件被触发拉!!!收到来自 demo2 的信息:" + value);
    },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Demo 组件

<template>
  <div class="demo">
    <h1>Demo</h1>
    <p>{{msg}}</p>
    <button type="button" @click="sendMsg"> 点击发送音讯给 App 组件 </button>
  </div>
</template>
<script>
export default {
  props: {showMsg: Function,},
  data() {
    return {msg: "hello, 大家好, 我是宁在春",};
  },
  methods: {sendMsg() {this.showMsg();
    },
  },
};
</script>
<style>
.demo {
  width: 200px;
  height: 200px;
  background-color: #1488f5;
}
</style>

demo1

<template>
  <div class="demo1">
    <h1>Demo2</h1>
    <span>{{msg}}</span>

    <button @click="sendAppMsg(msg)" type="button">
      点击发送音讯给 App 组件
    </button>
  </div>
</template>
<script>
export default {data() {
    return {msg: "大家好,我是来自 Demo2 的宁在春",};
  },
  methods: {sendAppMsg(message) {this.$emit("send-message", message);
    },
  },
};
</script>
<style>
.demo1 {
  width: 200px;
  height: 200px;
  background-color: pink;
}
</style>

明天是先写个头,下一节再说全局事件总线的原理。

后语

大家一起加油!!!如若文章中有不足之处,请大家及时指出,在此郑重感激。

纸上得来终觉浅,绝知此事要躬行。

大家好,我是博主 宁在春:主页

一名喜爱文艺却踏上编程这条路线的小青年。

心愿:咱们,待别日相见时,都已有所成

正文完
 0