v3 中的父子传参

第一种非语法糖格式:

<div>

        parent父组件

    {{ msg }}

        <mycom1 :msg="msg" a="1" b="2" c="3" d="4" @addd="addd"></mycom1>

    </div>

</template>

<script>

import mycom1 from "@/components/mycom1.vue";

import { ref } from "vue";

// 非语法糖的格式写

export default {

    components: {

        mycom1,

    },

    setup() {

        const  msg = ref(12);

        const addd = () => {

            msg.value++;

        }

        // return 返回出去,注意这里只能写一个return

        return {

                msg,

                addd

            }

    },

};

子组件

<template>

    <div>

        子组件

        <h3>{{ a }}--{{ b }}--{{ msg }}</h3>

        <!-- 添加事件 -->

        <button @click="add">添加</button>

    </div>

</template>

<script>

// 非语法糖结构的书写

export default {

    props: ['msg', 'a', 'b'],

    // setup中有两个参数,分别是props,context

    // props表示传过来的数据,打印的是 Proxy(Object)

    // context表示一个上下文对象

    // context中有四个属性,分别是attrs,slots,emit,expose

    // attrs表示的是父组件传递过来的props,

    // slots表示的是父组件传递过来的插槽

    // emit表示的是触发自定义事件

    setup(props, context) {

        console.log(props)

        console.log(context)

        //父传子点击添加父组件中的数据

        const add = () => {

            context.emit('addd')

        }

        // return 返回

        return { add }

    },

}

</script>

<style lang="scss" scoped></style>

第二种语法糖格式父子传参:

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值