vue父子通信

    科技2024-08-20  24

    自定义事件,子传父

    子 <template> <div> <button @click="toParent">点击传到父级</button> </div> </template> <script> export default { name: 'child', methods: { toParent () { this.$emit('fromChild', 'child') } } } </script> <template> <div> <p>子级传过来的值:{{childVal}}</p> <child @fromChild="getChild"></child> </div> </template> <script> import child from "@/components/child"; export default { name: 'parent', data () { return { childVal: '' } }, components: { child }, methods: { getChild (v) { this.childVal = v; } } } </script>
    Processed: 0.009, SQL: 8