【Vue3 项目中父子组件之间怎样互相传值、传递方法】

[复制链接]
发表于 2025-4-4 02:28:02 | 显示全部楼层 |阅读模式


在 Vue3 的项目开辟中,组件化是非常紧张的特性。父子组件之间的通讯是组件化开辟中常见的需求,本文将具体介绍 Vue3 中父子组件之间怎样互相传值以及传递方法。
一、父组件向子组件传值

1. 使用 props

props 是 Vue 中父组件向子组件传值最常用的方式。


  • 子组件定义 props:在子组件中,通过props选项来声明吸收的数据。比方,我们有一个ChildComponent.vue组件,想要吸收父组件传递的message数据:
  1. <template>
  2. <div>
  3. {{ message }}
  4. </div>
  5. </template>
  6. <script setup>
  7. const props = defineProps({
  8. message: String
  9. })
  10. </script>
复制代码


  • 父组件传递数据:在父组件中使用子组件时,通过属性绑定的方式传递数据。假设父组件是ParentComponent.vue:
  1. <template>
  2. <div>
  3. <ChildComponent :message="parentMessage" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import ChildComponent from './ChildComponent.vue'
  8. const parentMessage = 'Hello from parent'
  9. </script>
复制代码
这样,父组件的parentMessage就传递给了子组件的message。
2. 传递对象和数组

props 不仅可以传递根本类型的数据,也可以传递对象和数组。比方,父组件传递一个对象:
  1. <template>
  2. <div>
  3. <ChildComponent :user="userInfo" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import ChildComponent from './ChildComponent.vue'
  8. const userInfo = { name: 'John', age: 30 }
  9. </script>
复制代码
子组件吸收:
  1. <template>
  2. <div>
  3. <p>Name: {{ user.name }}</p>
  4. <p>Age: {{ user.age }}</p>
  5. </div>
  6. </template>
  7. <script setup>
  8. const props = defineProps({
  9. user: Object
  10. })
  11. </script>
复制代码
二、父组件向子组件传递方法

1. 通过 props 传递方法

父组件可以将方法作为 props 传递给子组件。


  • 父组件定义方法并传递:在父组件ParentComponent.vue中定义一个方法,并将其传递给子组件:
  1. <template>
  2. <div>
  3. <ChildComponent :handleClick="handleParentClick" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import ChildComponent from './ChildComponent.vue'
  8. const handleParentClick = () => {
  9. console.log('Parent method called from child')
  10. }
  11. </script>
复制代码


  • 子组件调用方法:在子组件ChildComponent.vue中通过props调用传递过来的方法:
  1. <template>
  2. <button @click="props.handleClick">Click me</button>
  3. </template>
  4. <script setup>
  5. const props = defineProps({
  6. handleClick: Function
  7. })
  8. </script>
复制代码
三、子组件向父组件传值

1. 使用自定义事件


通过$emit触发自定义事件是子组件向父组件传值的常用方式。


  • 子组件触发事件并传值:在子组件ChildComponent.vue中,使用defineEmits定义事件并触发,同时传递数据:
  1. <template>
  2. <button @click="sendDataToParent">Send data</button>
  3. </template>
  4. <script setup>
  5. const emit = defineEmits(['send-data'])
  6. const sendDataToParent = () => {
  7. const data = 'Hello from child'
  8. emit('send-data', data)
  9. }
  10. </script>
复制代码


  • 父组件监听事件并吸收数据:在父组件ParentComponent.vue中监听子组件触发的事件,并吸收数据:
  1. <template>
  2. <div>
  3. <ChildComponent @send-data="handleChildData" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import ChildComponent from './ChildComponent.vue'
  8. const handleChildData = (data) => {
  9. console.log('Received data from child:', data)
  10. }
  11. </script>
复制代码
四、子组件向父组件传递方法(间接实现)

固然子组件不能直接向父组件传递方法,但可以通过自定义事件来间接实现类似的效果。


  • 子组件触发事件并传递方法相干数据:子组件ChildComponent.vue触发事件并传递一些数据,这些数据可以用于父组件执行相应的逻辑:
  1. <template>
  2. <button @click="sendMethodData">Send method data</button>
  3. </template>
  4. <script setup>
  5. const emit = defineEmits(['send-method-data'])
  6. const sendMethodData = () => {
  7. const methodData = { param: 'example' }
  8. emit('send-method-data', methodData)
  9. }
  10. </script>
复制代码


  • 父组件根据吸收到的数据执行方法:父组件ParentComponent.vue根据吸收到的数据执行相应的方法:
  1. <template>
  2. <div>
  3. <ChildComponent @send-method-data="handleMethodData" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import ChildComponent from './ChildComponent.vue'
  8. const handleMethodData = (data) => {
  9. // 根据data执行相应的方法逻辑
  10. console.log('Received method data:', data)
  11. }
  12. </script>
复制代码
五、总结

Vue3 中父子组件之间的传值和传递方法是开辟中非常基础且紧张的技能。通过 props 可以实现父组件向子组件传值和传递方法,通过自定义事件可以实现子组件向父组件传值以及间接实现传递方法相干的逻辑。熟练把握这些通讯方式,可以或许帮助我们更好地构建复杂的 Vue3 应用步伐。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表