[Vue基础实战]子组件向父组件传值
参考代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>子组件向父组件传值</title> </head> <body> <div id="app"> <son @func="show"></son> <div class="parent"> {{name}}<span v-if="dataFormSon.name">有一个儿子叫{{dataFormSon.name}},今年{{dataFormSon.age}}岁了</span> </div> </div> <template id="tmp1"> <div class="son"> <h3>子组件</h3> <input type="button" value="找父亲" @click="findparent"> </div> </template> <script src="../js/vue.js"></script> <script> var wm = new Vue({ el: '#app', data:{ name:'老马', dataFormSon:{} }, methods: { show(data){ this.dataFormSon=data; } }, components: { son:{ template: '#tmp1', data() { return { sonmsg: {name:'小马',age:13} } }, methods: { findparent() { this.$emit('func', this.sonmsg); } } } } }); </script> </body> </html>