Vue3父子组件传值

本文介绍了在Vue3中使用scriptsetup语法糖进行父子组件间通信的三种方式:1)通过defineProps和defineEmits实现props和事件的传递;2)利用ref和defineExpose进行直接通信;3)使用provide和inject进行依赖注入。每种方法都有详细的代码示例。

本文基于 script setup 方式

defineProps和defineEmits实现父子组件传值

子组件

<template>
    <div>
        <!-- 方式一 直接调用$emit-->
        <el-button @click="$emit('sonClick', '子组件数据')">子按钮{{msg}}</el-button>
        <!-- 方式二 -->
        <el-button @click="handleGetMsg">子按钮{{msg}}</el-button>
    </div>
</template>
<script setup>
    import { ref } from 'vue'
    // defineProps声明props,返回一个对象,其中包含了传递给子组件的所有 props
    defineProps({
        msg: {
            type: String,
            default: ''
        }
    })
    // setup中不能使用$emit
    // 通过 defineEmits 宏来声明需要抛出的事件,返回一个等同于$emit的函数
    const emit = defineEmits(['sonClick'])
    function handleGetMsg() {
        emit("sonClick", "子组件向父组件传送的信息");
    }
</script>

父组件

<template>
    <div>
        <div>son:{{ myname }}</div>
        <son :msg="msg" @sonClick="sonClick"></son>
    </div>
</template>
<script setup>
    import { ref } from 'vue'
    import son from '@/components/son.vue' // 通过 <script setup>导入的组件都在模板中直接可用
    const msg = ref('父组件信息')
    const myname = ref('')
    function sonClick(val) {
        myname.value = val
    }
</script>


ref操作符实现父子组件传值

子组件

<template>
    <div>
        <el-button >子按钮</el-button>
    </div>
</template>
<script setup>
    import { ref } from 'vue'
    const a = ref('a')
    function sonEvent() {
        console.log("sonEvent")
    }
    // setup里面是默认私有的,父组件无法访问,需要通过defineExpose暴露出去,父组件才能访问
    defineExpose({
        sonEvent,
        a
    })
</script>

父组件

<template>
    <div>
        <son ref="myson"></son>
    </div>
</template>
<script setup>
    import { ref, onMounted } from 'vue'
    import son from '@/components/son.vue' // 不用注册
    // ref操作dom, myson必须和模板里ref的值一致
    const myson = ref(null)
    onMounted(() => {
        myson.value.sonEvent()
        console.log('a', myson.value.a)
    })
</script>

provide和inject依赖注入实现父子组件传值

父组件

<script setup>
import { ref, provide } from 'vue'
const count = ref(0)
function add() {
    count.value++
}
provide('obj', {
    count,
    add
})
</script>

子组件

<template>
    <div>
        <div>count: {{count}}</div>
        <el-button @click="add">加一</el-button>
    </div>
</template>
<script setup>
import { inject } from 'vue'
const { count, add } = inject('obj')
</script>
### Vue 3 父子组件教程 在 Vue 3 中,父子组件之间的数据传递主要通过 `props` 和 `emit` 实现。父组件可以通过 `props` 将数据传递给子组件,而子组件则可以通过 `emit` 触发事件将数据回父组件。 #### 父组件向子组件 父组件可以使用 `props` 向子组件传递数据。以下是一个完整的示例: ```vue <!-- ParentComponent.vue --> <template> <div> <h1>父组件</h1> <p>当前:{{ inputValue }}</p> <ChildComponent :value="inputValue" @update:value="handleInput" /> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import ChildComponent from &#39;./ChildComponent.vue&#39;; // 定义父组件的数据 const inputValue = ref(&#39;初始&#39;); // 处理子组件的更新事件 const handleInput = (newValue) => { inputValue.value = newValue; }; </script> ``` 在此示例中,父组件通过 `:value` 将数据传递给子组件,并监听 `update:value` 事件以接收子组件的更新[^1]。 #### 子组件向父组件 子组件可以通过 `emit` 方法将数据发送回父组件。以下是子组件的实现: ```vue <!-- ChildComponent.vue --> <template> <div> <h2>子组件</h2> <input :value="modelValue" @input="updateValue" /> </div> </template> <script setup> defineProps({ modelValue: { type: String, required: true } }); const emit = defineEmits([&#39;update:modelValue&#39;]); const updateValue = (event) => { emit(&#39;update:modelValue&#39;, event.target.value); }; </script> ``` 在这个例子中,子组件通过监听 `input` 事件来捕获用户的输入,并通过 `emit` 方法触发 `update:modelValue` 事件将新传递父组件。 #### 使用 `v-model` 简化双向绑定 Vue 3 支持通过 `v-model` 实现父子组件之间的双向绑定。以下是如何结合 `v-model` 使用的示例: ```vue <!-- ParentComponent.vue --> <template> <div> <h1>父组件</h1> <p>当前:{{ inputValue }}</p> <ChildComponent v-model="inputValue" /> </div> </template> <script setup> import { ref } from &#39;vue&#39;; import ChildComponent from &#39;./ChildComponent.vue&#39;; const inputValue = ref(&#39;初始&#39;); </script> ``` ```vue <!-- ChildComponent.vue --> <template> <div> <h2>子组件</h2> <input :value="modelValue" @input="updateValue" /> </div> </template> <script setup> defineProps({ modelValue: { type: String, required: true } }); const emit = defineEmits([&#39;update:modelValue&#39;]); const updateValue = (event) => { emit(&#39;update:modelValue&#39;, event.target.value); }; </script> ``` 通过 `v-model`,父组件和子组件之间的数据绑定变得更加简洁和直观。 #### 注意事项 - 父组件向子组件传递数据时,确保 `props` 的类型与数据一致。 - 子组件向父组件传递数据时,推荐使用标准化的事件名称(如 `update:modelValue`),以便代码更具可读性[^2]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值