首页 > 其他分享 >Vue中全局事件总线

Vue中全局事件总线

时间:2023-01-09 22:25:02浏览次数:57  
标签:School Vue name App 总线 vue 全局

Vue中全局事件总线


1:全局事件总线

image



2:示例代码结构


image



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:展示界面效果

image

标签:School,Vue,name,App,总线,vue,全局
From: https://www.cnblogs.com/ios9/p/17038682.html

相关文章

  • VUE 防抖简单实现
    防抖代码实现:exportclassDebounce{staticcurrent:Debounce=newDebounce();staticasyncInvoke<T>(func:()=>Promise<T>,timeout:number=300)......
  • 数据代理_3Vue中的数据代理
    数据代理_3Vue中的数据代理<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Vue中的数据代理</title><scriptsrc="../js/vue.js">......
  • Vue——computed计算属性 例子
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <metaname="viewport"......
  • Vue——watch侦听属性 例子
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <metaname="viewport"......
  • Vue判断数组元素是否为undefined
    问题:现在有这样一个数组,没有第一个元素,如何判断该位置为空控制台输出为undefinde首先尝试array[0]===undefined,可以需要修改为array[0]==="undefinde"这样不行......
  • avue的一些踩坑记录
    avue可以说前端开发的一大利器了。给大家封装了一些非常易用的组件,尤其对于crud以及表单的操作。但是封装易用的同时,也会导致一些api不能方便的调用,尤其是多组件嵌套交互......
  • node nodejs 绿色版 添加环境变量 淘宝镜像源 全局安装文件夹 npm_global npm_cache
    解压文件和创建两个文件夹配置一个文件将上面的两个文件夹路径添加到文件中同时添加npm的淘宝镜像源cache=D:\node-v16.17.0-win-x64\node_cacheprefix=D:\node-v......
  • vue3 中动态绑定 img src 问题
    vite 官方默认的配置,如果资源文件在assets文件夹打包后会把图片名加上hash值,但是直接通过:src="imgSrc"方式引入并不会在打包的时候解析,导致开发环境可以正常引入,打包后......
  • [VueJsDev] 快速入门 - vue项目根目录配置文件
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.htmlvue项目根目录配置文件:::details目录目录vue项目根目录配置文件Part.1:package.json......
  • [VueJsDev] 基础知识 - 常见编码集
    [VueJsDev]目录列表https://www.cnblogs.com/pengchenggang/p/17037320.html常用编码集:::details目录目录常用编码集Part.1:Unicode编码Part.2:ASCII编码Par......