分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

vue.js学习笔记(3)— $emit 作用快速了解

发布时间:2023-09-06 01:51责任编辑:熊小新关键词:js

$emit 触发当前实例上的事件,也可以简单的理解为触发父组件上的事件(向上冒泡),实例(当前实例)如下:

<!DOCTYPE html><html> ???<head> ???????<meta charset="UTF-8"> ???????<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> ???????<title>session</title> ???????<script src="https://unpkg.com/vue/dist/vue.js"></script> ???????<style type="text/css"> ???????????#session { ???????????????width: 600px; ???????????????margin: 0 auto; ???????????????text-align: center; ???????????} ???????</style> ???</head> ???<body> ???????<div id="counter-event-example"> ?????????<button-counter v-on:increment="incrementTotal"></button-counter> ???????</div> ???????????????<script> ???????????Vue.component(‘button-counter‘, { ?????????????template: ‘<button v-on:click="incrementCounter">点我哒</button>‘, ?????????????methods: { ???????????????incrementCounter: function () { ???????????????????alert("我是组件"); ?????????????????????this.$emit(‘increment‘) ???????????????} ?????????????} ???????????}) ???????????????????????new Vue({ ?????????????el: ‘#counter-event-example‘, ?????????????methods: { ???????????????incrementTotal: function () { ???????????????????alert("我是当前实例"); ???????????????} ?????????????} ???????????}) ???????</script> ???</body></html>

当点击“点我哒”的时候,会依次跳出下面两个 alert,根据前后跳出的顺序我们可以清楚的看到事件的前后触发顺序,如图:

这样是不是就很清楚的理解 $emit 的作用了,:)

vue.js学习笔记(3)— $emit 作用快速了解

原文地址:https://www.cnblogs.com/zxn-9588/p/8969014.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved