① 子传父
子组件需要传递的数据 this.$emit('{func1}', data)
this.$emit('func1', data)
其中func1对应到父组件中,在父组件引入子的模块中使用 v-on绑定
对应上面的传值即 v-on:'func1'='fatherFunc'
<ChildComp1
id="ChildComp1"
v-on:func1="fatherFunc"
/>
②父传子
在父组件的data中,定义return的字段,比如 param1
// 父组件js
data () {
return {
param1: {},
}
},
// 父组件template绑定对应的值和方法
<div>
<ChildComp2
id="ChildComp2"
v-bind:param1="param1"
/>
</div>
然后在需要接收值的子组件中,使用props接收,即在子组件的props里面定义同名的param1
动态修改的话,子组件watch里写上对应字段的监听,以及对应的computed
// 子组件
watch: {
param1: {
handler: function (val) {
this.setBusiness(val)
},
},
},
computed: {
getParam1 () {
if (this.param1 !== null) {
return this.param1
} else {
return null
}
},
},
watch中的方法即对应动态业务的动作
③兄弟组件传值
不适用vuex,逻辑是由子传给父,又同步传给兄弟,代码大致如上面几张图的布局,此时用上面第二张图的代码里的fatherFunc对数据进行处理