兄弟組件傳數據:
一: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} 不推薦