body {background-color: #c3c3c3}

组件三种情况传参,兄弟组件传输数据

兄弟组件传数据:

一:main.js中 定义全局事件:

new Vue({
  render: h => h(App),
  // 传参用
  beforeCreate() {
  	Vue.prototype.$bus=this   //全局事件总线
  }
  
}).$mount('#app')

二 事件发送数据组件:   

change(){			
		this.$bus.$emit('hello', Date.now())
		}s 

三 接收数据组件

mounted(){
			this.$bus.$on('hello',(data)=>{
				this.name=data
			
			})
		} 

 

还一个办法 在main.js 中直接定义 window.datas={a:1}   不推荐

 

 

 

在父组件 绑定一个事件 【 其实是在父组件的VC 绑定 跟随VC】

在子组件 通过事件去触发  this.$emit("" ,"")

 

 

 

posted @ 2023-12-14 00:23  最美胡萝卜  阅读(10)  评论(0)    收藏  举报
body {background-color: #c3c3c3}