详解vue中在父组件点击按钮触发子组件的事件
网络编程 2021-07-04 14:07www.168986.cn编程入门
这篇文章主要介绍了详解vue中在父组件点击按钮触发子组件的事件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
我把这个实例分为几个步骤解读:
1、父组件的button元素绑定click事件,该事件指向notify方法
2、给子组件注册一个ref=“child”
3、父组件的notify的方法在处理时,使用了$refs.child把事件传递给子组件的parentMsg方法,同时携带着父组件中的参数msg
4、子组件接收到父组件的事件后,调用了parentMsg方法,把接收到的msg放到message数组中
父组件
<template> <div id="app"> <!--父组件--> <input v-model="msg" /> <button v-on:click="notify">广播事件</button> <!--子组件--> <popup ref="child"></popup> </div> </template> <script> import popup from "@/ponents/popup"; export default { name: "app", data: function () { return { msg: "", }; }, ponents: { popup, }, methods: { notify: function () { if (this.msg.trim()) { this.$refs.child.parentMsg(this.msg); } }, }, }; </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-: 60px; } </style>
子组件
<template> <div> <ul> <li v-for="item in messages">父组件输入了:{{ item }}</li> </ul> </div> </template> <style> body { background-color: #ffffff; } </style> <script> export default { name: "popup", data: function () { return { messages: [], }; }, methods: { parentMsg: function (msg) { this.messages.push(msg); }, }, }; </script>
到此这篇关于详解vue中在父组件点击按钮触发子组件的事件的文章就介绍到这了,更多相关vue 父组件触发子组件内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程