vue3 setup语法糖父子组件传值

一、父传子

父组件

<template>
  <div class="hello">
  我是父组件
  <!-- 父组件通过:变量(这里是info)绑定值  也可以不用:常量例如info="123" -->
   <Child :info="parentMsg"></Child>
  </div>
</template>

<script setup>
import Child from './Child'
import {ref} from 'vue'
const parentMsg=ref('父组件传递值是a')
</script>

子组件

<template>
  //通过props.传过来的值来使用
  <div>我是子组件拿到了父组件的值是{{props.info}}</div>
</template>
 
<script setup>
import { toRefs, defineProps } from 'vue'
const props = defineProps({
  //子组件接收父组件传递过来的值
  info: String,
})
</script>

 二、子传父

子组件

<template>
  <button @click="click">点击子组件</button>
</template>
 
<script setup>
import { defineEmits } from 'vue'
// 使用defineEmits创建名称,接受一个数组声明emit可以传的值
const emit = defineEmits(['clickChild'])
const click=()=>{
  let param={
    content:'b'
  }
  //传递给父组件 引号中的值必须是emit中声明了的
  emit('clickChild',param)
}
</script>

父组件

<template>
  <div class="hello">
  <!-- clickChild是子组件传值时声明的类型 -->
   <Child  @clickChild="clickEven"></Child>
 </div>
</template>
 
<script setup>
import Child from './Child'
import {ref} from 'vue'
const clickEven=(val)=>{
  console.log(val);
}
</script>

三、父组件使用子组件中的属性值

子组件

<template>
  <div>
        <p>性别:{{ sex}}</p>
  </div>
</template>
 
<script setup>
import { reactive, ref,defineExpose } from "vue";
let sex=ref('男')
let info=reactive({
    like:'王者荣耀',
    age:18
})
defineExpose({sex, info}) //defineExpose({})包裹想给父组件使用的值
</script>

父组件

<template>
  <div class="hello">
    //要给子组件绑定一个ref
     <Child ref="testcomRef"></Child>
     <button @click="getSonHander">获取子组件中的数据</button>
   </div>
</template>

<script setup>
import Child from './Child'
import {ref} from 'vue'
//声明给子组件绑定的ref
const testcomRef = ref()
const getSonHander=()=>{
  console.log('获取子组件中的性别', testcomRef.value.sex );
  console.log('获取子组件中的其他信息', testcomRef.value.info )
}
</script>

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中,你可以使用`setup`函数来实现父子组件之间的传值。`setup`函数是在组件创建之前执行的,它接收两个参数:`props`和`context`。 要在组件向子组件传递值,首先需要在组件中将值作为属性传递给子组件。然后,在子组件的`setup`函数中可以通过`props`参数访问到组件传递过来的值。 以下是一个简单的示例: ```vue <template> <div> <p>组件传递的值: {{ message }}</p> <ChildComponent :message="message" /> </div> </template> <script> import { ref } from 'vue'; import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, setup() { const message = ref('Hello from parent'); return { message }; } }; </script> ``` 在上面的示例中,组件通过`:message="message"`将`message`作为属性传递给子组件。在子组件的`setup`函数中,通过`props`参数可以访问到组件传递过来的值。 ```vue <template> <div> <p>子组件接收到的值: {{ message }}</p> </div> </template> <script> export default { props: { message: { type: String, required: true } }, setup(props) { // 可以通过 props.message 访问到组件传递过来的值 return { message: props.message }; } }; </script> ``` 在子组件中,可以通过`props`对象访问到组件传递过来的值,并将其赋值给一个本地变量,以便在模板中使用。 这样就实现了父子组件之间的传值。注意,`setup`函数中返回的是一个对象,该对象中的属性可以在模板中直接使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值