一、父子之间传值:(props)
<swiperT :list='swiperData'></swiperT> 父组件-:绑定数据
props:['list'], 子组件,用props接收,也可以对象写法
props:{
list:{
type : Object,
required:true,
}
}
二、子父之间传值:(得绑定时间@+方法,然后用$.emit)
<p class="num" @click="dateClick(item,index)"> 子组件
methods: {
dateClick(item,index){
this.active = index;
this.$emit('dateClick',item);
//this.$emit("function",param); //其中function为父组件定义函数,param为需要传递参数
}
},
<dateLine @dateClick='点击绑定的事件名字'></dateLine> 父组件
三、子子之间传值:vuex或者new vue的实例来实现
vuex的话:
通过state定义变量,然后action获取后台数据,在mutions对后台数据进行加工,最后用getters给发送出来
创建一个公共的vue实例,空的实例不会影响,
1.在main.js中注册全局的bus
Vue.prototype.bus=new Vue();
2.在组建中使用
子组建使用:this.bus.$emit('自定义事件名',data)
methods:{
handleClicks(){
this.bus.$emit('openMenu',true)
}
}
父组建使用: this.bus.$on("自定义事件名", msg => {})
mounted() {
this.bus.$on("openMenu", msg => {
this.show = msg;
});
}