vue3-setup语法糖 父子组件传值

本文介绍了在Vue3中如何进行父子组件间的通信,包括通过props从父组件向子组件传递值,子组件使用setup语法糖接收和使用这些值;子组件通过defineEmits定义并触发事件向父组件传递值,以及父组件如何接收和处理这些事件;最后,演示了如何通过defineExpose暴露子组件的属性,使父组件能访问到子组件的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建子组件 Child.vue

一、父组件向子组件传值

父组件向子组件传值的时候,子组件是通过props来接收的,然后以变量的形式将props传递到setup语法糖中使用

1、父组件传递方式 

<template>
  <div class="hello">
  我是父组件
  <!-- 父组件通过:变量(这里是info)绑定值 -->
   <Child :info="parentMsg"></Child>
  </div>
</template>
 
<script setup>
import Child from './Child'
import {ref} from 'vue'
const parentMsg=ref('父组件传递值是a')
 
</script>
 
<style scoped>
 .hello{
   color:red;
  }
</style>

2、子组件接收方式和使用

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

 二、子组件向父组件传值

vue3的setup语法糖的是defineEmits

 1、子组件的传递方式

<template>
  <button @click="clickChild">点击子组件</button>
</template>
 
<script setup>
import { defineEmits } from 'vue'
// 使用defineEmits创建名称,接受一个数组
const emit = defineEmits(['clickChild'])
const clickChild=()=>{
  let param={
    content:'b'
  }
  //传递给父组件
  emit('clickChild',param)
}
</script>
 
<style>
 
</style>

2、父组件接收与使用

<template>
  <div class="hello">
  我是父组件
  <!-- clickChild是子组件绑定的事件,click是父组件接受方式 -->
   <Child  @clickChild="clickEven"></Child>
 <p>子组件传递的值是 {{result}}</p>
 </div>
</template>
 
<script setup>
import Child from './Child'
import {ref} from 'vue'
const result=ref('')
const clickEven=(val)=>{
  console.log(val);
  result.value=val.content
}
</script>
 
<style scoped>
 
</style>

 传值 

子组件

<template>
  <div>
    <button @click="handleIncrement">Increment</button>
    <button @click="handleDecrement">Decrement</button>
  </div>
</template>

<script setup lang="ts">
const emit = defineEmits(['increment', 'decrement']);

const handleIncrement = () => {
  emit('increment');
};

const handleDecrement = () => {
  emit('decrement');
};
</script>

在父组件中,监听子组件触发的 increment 和 decrement 事件,并通过相应的处理函数来更新计数器

<template>
  <div>
    <p>Counter: {{ counter }}</p>
    <ChildComponent @increment="increment" @decrement="decrement" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

const counter = ref(0);

const increment = () => {
  counter.value++;
};

const decrement = () => {
  counter.value--;
};
</script>

传参

子组件中,定义一个事件 updateValue,它会传递一个数值参数

<template>
  <div>
    <button @click="update(1)">Increment</button>
    <button @click="update(-1)">Decrement</button>
  </div>
</template>

<script setup lang="ts">
const emit = defineEmits<{ (e: 'updateValue', value: number): void }>();

const update = (value: number) => {
  emit('updateValue', value);
};
</script>

 在父组件中,接收 updateValue 事件,并使用传递的参数更新计数器

<template>
  <div>
    <p>Counter: {{ counter }}</p>
    <ChildComponent @updateValue="updateCounter" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

const counter = ref(0);

const updateCounter = (value: number) => {
  counter.value += value;
};
</script>

传对象

 

在对象中,键是事件名,值是一个函数,该函数的参数就是事件的参数

子组件 使用对象语法定义事件和参数类型

<template>
  <div>
    <button @click="update(1)">Increment</button>
    <button @click="update(-1)">Decrement</button>
  </div>
</template>

<script setup lang="ts">
const emit = defineEmits({
  updateValue: (value: number) => true,
});

const update = (value: number) => {
  emit('updateValue', value);
};
</script>

在父组件中,接收 updateValue 事件,并使用传递的参数更新计数器

 

<template>
  <div>
    <p>Counter: {{ counter }}</p>
    <ChildComponent @updateValue="updateCounter" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

const counter = ref(0);

const updateCounter = (value: number) => {
  counter.value += value;
};
</script>

三、父组件获取子组件中的属性值

当时用语法糖时,需要将组建的属性及方法通过defineExpose导出,父组件才能访问到数据,否则拿不到子组件的数据

 1、子组件的传递方式

<template>
  <div>
        <h2> 我是子组件</h2>
        <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})
</script>
 
<style>
 
</style>

2、父组件显示方式

<template>
  <div class="hello">
  我是父组件
   <Child ref="testcomRef"></Child>
<button @click="getSonHander">获取子组件中的数据</button>
 </div>
</template>
 
<script setup>
import Child from './Child'
import {ref} from 'vue'
const testcomRef = ref()
const getSonHander=()=>{
  console.log('获取子组件中的性别', testcomRef.value.sex );
    console.log('获取子组件中的其他信息', testcomRef.value.info )
}
</script>
 
<style scoped>
 
</style>

Vue 3中,你可以使用`setup`函数来实现父子组件之间的。`setup`函数是在组件创建之前执行的,它接收两个参数:`props`和`context`。 要在父组件中向子组件,首先需要在父组件中将作为属性递给子组件。然后,在子组件的`setup`函数中可以通过`props`参数访问到父组件递过来的。 以下是一个简单的示例: ```vue <template> <div> <p>父组件递的: {{ message }}</p> <ChildComponent :message="message" /> </div> </template> <script> import { ref } from &#39;vue&#39;; import ChildComponent from &#39;./ChildComponent.vue&#39;; export default { components: { ChildComponent }, setup() { const message = ref(&#39;Hello from parent&#39;); 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`函数中返回的是一个对象,该对象中的属性可以在模板中直接使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值