Vue || Props传值 父传子,子传父

1.自己写的提示子,只共参考,只是个人理解

一:这个是父组件,在view中创建index.vue

<template>
    <div class="fathere">
        <!-- handle="accept" @han="hcc" -->
        <Son txt="1222" :list="list" :send="send" ref="Text"></Son>
        <!-- <input type="text" ref="Text"> -->
    </div>
</template>
<script lang="ts" setup>
import Son from '@/components/son.vue';
import { onMounted, ref } from "vue"




// 父传子:父组件传一个特定的数组(list) :list="list" 通过这样就可以传过去了,在子组件中定义类型在通过  props.list 进行输出到控制台
// 父组件传值   创建一个list 数组并且定义类型
const list = ref<string[]>(['父数据', '父数据', '父数据',])

// 子组件传过来的参数   函数形式
const send = (data: string) => {
    console.log("data", data);

}



// interface VueCom {
//     value: HTMLDivElement,
//     arr: number[]
// }

// const Text = ref<VueCom | null>(null)

// onMounted(() => {
    // console.log(Text.value!.arr, 111);

// })

// 自定义组件传参 事件
// const accept = (data: string) => {
//     console.log(data, accept);

// }
// const hcc = (data: string) => {
//     console.log(data, hcc);

// }







</script>

二:在components中创建子组件

<template>
    <div class="son">
        this is son<br>
        <!-- <button @click="FnSend">点击</button><br>
        <button @click="Fnhan">点击</button> -->

    </div>
</template>
<script lang="ts" setup>
import { defineProps, defineEmits } from 'vue';
import { ref } from "vue"



const emit = defineEmits(['handle', 'han'])

const props = defineProps({
    txt: String,
    list: Array,
    send: Function,
})
console.log(props.list);



const num: string[] = ["子数据", "子数据","子数据"]
console.log(props.send!(num))
// 子传父  : 父组件中传给子组件一个函数,通过这个函数来调用,子组件中的数据就可以通过这个函数传过去



// 事件传参  click 
// const FnSend = () => {
//     emit('handle', [21, 34, 56])
// }
// const Fnhan = () => {
//     emit('han', [1, 2, 3])
// }






</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值