一、父组件 传 子组件( props )
<Swiper :title='title' :num='num'></Swiper>
export default{
1、 props: ['title','num']
2、 props: {
title: String,
num: Number
}
}
二、子组件 传 父组件(自定义事件)
<button @click="btn">点击传值</button>
methods: {
btn() {
this.$emit('changeStr', this.str)
}
}
<Child @changeStr="receiveStr"></Child>
methods: {
receiveStr(val){
this.str = val
}
}
三、兄弟组件传值 (bus)
import Vue from 'vue';
export default new Vue;
import bus from './bus'
<button @click='clickBtn'>点击传值</button>
methods: {
clickBtn() {
bus.$emit('changeMax', this.maxStr)
}
}
import bus from './bus'
created() {
bus.$on('changeMax', (res) => {
console.log(res);
})
},
beforeDestroy() {
bus.$off('changeMax');
},
const bus = new Vue()
Vue.prototype.$bus = bus;
this.$bus.$emit
this.$bus.$on
this.$bus.$off