This article introduces that the parent component listens to child component events and sends back information. It is shared with you. I hope this article will be helpful to you.
Use vm.$emit
1. Reference child components in parent component
<child @from-child-msg="listenChildMsg"></child >
2. Use $emit to send events in subcomponents
this.$emit('from-child-msg', 'This is the message passed by the child component');
demo
<!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> <style> * { padding: 0; margin: 0; } input, select { height: 30px; } </style> </head> <body> <div > <child value="name" @msg-from-child="getMsgFromChild"></child> </div> <script src="/ajax/libs/vue/2.2.6/"></script> <script> ('child', { data: function () { return { val: } }, props: ['value'], methods: { handleClick () { this.$emit('msg-from-child', ) } }, template: ` <div><input type="text" v-model="val"><button type="button" @click="handleClick">Sure</button></div> ` }) new Vue ({ el: '#app', data: { }, methods: { getMsgFromChild (v) { alert('msg: ' + v) } } }) </script> </body> </html>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.