Vue子组件向父组件传值 meowrain 收录于 前端 - Vue2025-01-03 约 200 字 预计阅读 1 分钟 注意 本文最后更新于 2025-01-03,文中内容可能已过时。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Helloworld.vue <template> <div> <h1>Helloworld </h1> <button @click="trigEmit">点击触发父组件传来的函数</button> <button @click="()=>{ console.log(props.name) }">点击打印从父组件传来的值</button> </div> </template> <script setup> /** * * defineProps:用于声明接收的数据(从父组件传递的 props)。 defineEmits:用于声明和触发事件(子组件通知父组件)。 */ import { defineProps,defineEmits } from 'vue'; const props = defineProps(['name']) const emit = defineEmits(["fun"]) const trigEmit = ()=>{ emit("fun","meowrain") } </script> <style scoped></style> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 app.vue <script setup> import { ref } from 'vue'; import HelloWorld from './components/HelloWorld.vue' const name = ref("meowrain") const printHello = (arg)=>{ console.log("hello " + arg) } </script> <template> <div> <!-- @ 传递方法,:传递值 --> <HelloWorld :name="name" @fun="printHello"/> </div> </template> <style scoped> </style> 相关内容 使用vue Cropper编写头像上传组件 Vue項目導入Electron Vite中加载环境变量 跨域问题以及解决方案 Vue笔记13-Vue Transition组件