<template>
<div v-title :data-title="title">
<div>car---兄弟组件传值</div>
<div>下面显示的是brother1向brother2页面进行兄弟组件传值:</div>
<brother1></brother1>
<brother2></brother2>
</div>
</template>
<script>
import brother1 from '../components/brother1'
import brother2 from '../components/brother2'
export default {
components:{brother1,brother2},
data() {
return {
title:'页面名称'
}
},
methods: {
},
created: function () {
}
}
</script>
组件brother1.vue页面代码:
<template>
<div>
<div @click="emitCar()">
兄弟页面brother1---------点击传值到brother2
</div>
</div>
</template>
<script>
import brother from '../api/brother.js';
export default {
data() {
return {
};
},
methods: {
emitCar(){
brother.$emit('to-car','我是兄弟组件1要传的值')
},
},
};
</script>
组件brother2.vue页面代码:
<template>
<div>
兄弟页面brother2------兄弟组件1传值“{{selfContent}}”到兄弟组件2了
</div>
</template>
<script>
import brother from '../api/brother.js';
export default {
data() {
return {
selfContent:''
}
},
methods: {
},
created: function () {
brother.$on('to-car',(data)=>{
console.log(data)
this.selfContent = data;
})
},
}
</script>
第一种方法----bother.js页面代码:
import Vue from 'vue'
var brother = new Vue();
export default brother
第二种方法----main.js页面代码:
Vue.prototype.bus = new Vue();