vue父子通信 (子 --- 父)

27 阅读1分钟
|

| - | ---------------------------------------------------------------------- | | | | | | | | | | | | | | | | | | Document | | | | | | | | |

| | | | | |
| | | | | | , | | | data(){ | | | return { | | | msg: '这是子组件数据' | | | } | | | }, | | | methods: { | | | emit(){ | | | // 触发子组件的自定义事件 | | | // emit(事件[,携带的参数])this.emit('事件名'[,携带的参数]) | | | this.emit('biubiubiu', this.msg); | | | } | | | } | | | } | | | // 页面 | | | const Home = { | | | template: | | | <div> | | | 这是home页 | | | {{ msg }} | | | <hr/> | | | <common-head @biubiubiu="fn"></common-head> | | | </div> | | |, | | | data(){ | | | return { | | | msg:"" | | | } | | | }, | | | methods: { | | | fn(msg){ | | | console.log('我收到了', msg); | | | this.msg = msg; | | | } | | | }, | | | components: { | | | CommonHead | | | } | | | } | | | | | | Vue.component('HomePage', Home) | | | const vm = new Vue({ | | | el: '#app' | | | }) | | | | | | | | |