代码
提交代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <person></person> <detail /> </div> </body> <script src="https://unpkg.com/vue/dist/vue.js"></script> <script type="text/javascript"> let bus = new Vue() Vue.prototype.bus = bus Vue.component('person', { template: '<div><div>姓名:<input type="text" v-model="name"/></div><div>年龄:<input type="text" v-model="count"/></div><button @click="modify">修改</button></div>', data() { return { name: '句号', count: 18 } }, methods: { modify() { this.bus.$emit('modify', {name: this.name, count: this.count}) } } }) Vue.component('detail', { template: '<div>我是:{{name}}, 我今年 {{count}}岁。</div>', data() { return { name: '句号', count: 18 } }, mounted() { this.bus.$on('modify', (detail) => { this.name = detail.name this.count = detail.count }) } }) var vm = new Vue({ el: '#app', methods: { } }) </script> </html>
运行结果