vue平级访问数据

<template>
  <div class="a">
    <h2>this is A</h2>
    <span>{{ msg }}</span>
  </div>
</template>

<script>
import Bus from '../Tools/EventBusTool';
export default {
    data(){
        return{
            msg:''
        }
    },
    created(){
        Bus.$on('sent',(msg) =>{
            this.msg=msg
        })
    }
}
</script>

<style>
.a{
  border: 100px;
  background-color: aqua;
}
</style>
<template>
  <div class="b">
    <h2>this is B</h2>
    <button @click="sent">sent</button>
  </div>
</template>

<script>
import Bus from '../Tools/EventBusTool';
export default {
    methods:{
        sent(){
            Bus.$emit('sent','需要发送的消息')
        }
    }
}
</script>

<style scoped>
.b{
  border: 100px;
}
</style>
eventBusTool
import Vue from "vue";
const Bus=new Vue()
export default Bus

 

posted @ 2023-12-14 17:45  超爱彬宝同学  阅读(7)  评论(0)    收藏  举报