新建until下的js文件,new Vue
import Vue from "vue"
export default new Vue
错误写法
触发
<template>
<div>
<button @click="fun()">兄弟通信</button>
</div>
</template>
<script>
import Bus from "@/until/indexBus.js"
export default {
data(){
return {
demoatext:"我是demoa的数据"
}
},
methods: {
fun(){
Bus.$emit("pao",this.demoatext)
this.$router.push('/two')
}
},
// destroyed() {
// Bus.$emit("pao",this.demoatext)
// },
}
</script>
接收
<template>
<div>two-----{{test}}</div>
</template>
<script>
import Bus from "@/until/indexBus.js";
export default {
data() {
return {
test: "11111",
};
},
mounted() {
Bus.$on("pao", (val) => {
console.log( val);
this.test=val
console.log(this.test);
})
}
};
</script>
错误之处:test以旧是11111没有刷新传过来的值,但是可以console到
正确写法
写入
<template>
<div>
one
<button @click="fun()">兄弟通信</button>
</div>
</template>
<script>
import Bus from "@/until/indexBus.js"
export default {
data(){
return {
demoatext:"我是demoa的数据"
}
},
methods: {
fun(){
this.$router.push('/two')
}
},
destroyed() {
Bus.$emit("pao",this.demoatext)
},
}
</script>
接收
<template>
<div>two-----{{test}}
</div>
</template>
<script>
import Bus from "@/until/indexBus.js";
export default {
data() {
return {
test: "11111",
};
},
created() {
Bus.$on("pao", (val) => {
console.log( val);
this.test=val
console.log(this.test);
})
},
};
</script>