Vue中全局事件总线
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com
正文
Vue中全局事件总线
1:全局事件总线
2:示例代码结构
3:代码内容
vue.config.js
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ transpileDependencies: true, lintOnSave:false /*关闭语法检查*/ })
main.js
//引入Vue import Vue from 'vue' //引入App import App from './App.vue' //关闭Vue生产提示 Vue.config.productionTip=false; /* // Vue的线程总线的配置数据存储中间对象。 const vcObj=Vue.extend({}); const vc=new vcObj(); Vue.prototype.x=vc; */ // 创建Vm const vm = new Vue( { el:'#app', render: (h) => h(App), beforeCreate () { // Vue的线程总线的配置数据存储中间对象 Vue.prototype.$bus=this; //安装全局事件总线 } });
App.vue
<template> <div> <hr/> <div class="divCss"> <h1 class="h1Css">{{msg}}</h1> <hr/><br/> <School /> <hr/><br/> <Student /> <hr/><br/> </div> </div> </template>
<script> import School from './components/School.vue'; import Student from './components/Student.vue'; export default { name:'App', data(){ return{ msg:"Vue_线程总线", studentTomName:'' } }, components:{ Student, School } } </script>
<!-- 1:指定Css 样式的编写方式 less 最大特点:内容可以嵌套着写 --> <style lang="less"> .divCss{ background-color: chocolate; margin: auto; padding: 20px; .h1Css{ font-size: 36px; color: white; } } </style>
School.vue
<!-- ## scoped样式 1. 作用:让样式在局部生效,防止冲突。 2. 写法:```<style scoped>``` --> <template> <div class="ClassStyle"> <h1 style="color:red">{{msg}}</h1> <h2>学校名称:{{name }}</h2> <h2>学校地址:{{address}}</h2> </div> </template>
<script> export default { name:'School', data () { return { msg:'学校信息', name:'华南师范大学', address:'广州市天河区中山大道西55号' } }, mounted () { console.log("School",this.$bus); this.$bus.$on('hello',(data)=>{ console.log('我是School组件, 收到了数据:',this.name); }); }, beforeDestroy () { this.$bus.$off('hello'); } } </script>
<!--scoped 控制组件的Css样式为局部样式,不会为同名的cs样式名的冲突导致样式覆盖 --> <style scoped> .ClassStyle{ background-color: aquamarine; padding: 5px; } </style>
Student.vue
<!-- ## scoped样式 1. 作用:让样式在局部生效,防止冲突。 2. 写法:```<style scoped>``` --> <template> <div class="ClassStyle"> <h1 style="color:red">{{msg}}</h1> <h2>学生名称:{{name}}</h2> <h2>学生性别:{{sex}}</h2> <button @click="sendStudentNameToSchool">把学生名给School组件</button> </div> </template>
<script> export default { name:'Student', data () { return { msg:'学生信息:One', name:'向北', sex:'男', score:12 } }, mounted () { console.log("Student",this.$bus); }, methods:{ sendStudentNameToSchool(){ this.$bus.$emit('hello',666); } } } </script>
<!--scoped 控制组件的Css样式为局部样式,不会为同名的cs样式名的冲突导致样式覆盖 --> <style scoped> .ClassStyle{ background-color:lightskyblue; padding: 5px; } </style>
4:展示界面效果
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?