父子组件传递值
父组件:
<template>
<div>
<h4>我是父组件</h4>
子组件传递过来的值:{{ lisiC }}
<D @lisiD='getVal' :zhangsanC='zhangsanC'></D>
</div>
</template>
<script>
import D from './D.vue'
export default {
name: 'C',
components: {
D
},
props: {},
data() {
return {
zhangsanC: `敢问二位施主,什么是真经?`,
lisiC: ''
}
},
methods: {
getVal(val) {
this.lisiC = val
}
}
}
</script>
子组件
<template>
<div>
<h4>我是子组件</h4>
父组件传递过来的值:{{ zhangsanC }}
</div>
</template>
<script>
export default {
name: 'D',
props: ['zhangsanC'],
data() {
return {
lisiD: '所谓真经,就是能够达到寂空涅槃的究竟法门'
}
},
mounted() {
this.add()
},
methods: {
add(){
this.$emit('lisiD',this.lisiD)
}
}
}
</script>
兄弟之间传值
eventBus模块
import Vue from 'vue'
export default new Vue()
A组件
<template>
<div>
<h4>我是兄弟组件A</h4>
B组件传递过来的值:{{ xiaohongA }}
</div>
</template>
<script>
import bus from '../store/eventBus.js'
export default {
name: 'A',
data() {
return {
xiaomingA: `美女是魔`,
xiaohongA:''
}
},
created() {
bus.$on('xiaohongB',(val) =>{
this.xiaohongA = val
})
},
mounted() {
this.send()
},
methods: {
send() {
bus.$emit('xiaomingA', this.xiaomingA)
}
}
}
</script>
B组件
<template>
<div>
<h4>我是兄弟组件B</h4>
A组件传递过来的值:{{ xiaomingB }}
</div>
</template>
<script>
import bus from '../store/eventBus.js'
export default {
name: 'B',
data() {
return {
xiaomingB:'',
xiaohongB:'可悟不可修 '
}
},
created() {
bus.$on('xiaomingA',(val) => {
this.xiaomingB = val
})
},
mounted() {
this.send()
},
methods: {
send(){
bus.$emit('xiaohongB',this.xiaohongB)
}
}
}
</script>
目录结构

最终效果图
