vue3 父子组件间通讯

Posted on   嗷呜~  阅读(34)  评论(0编辑  收藏  举报

1、父组件向子组件传值

父组件

1
2
<fitSteps :stepActive="stepActive">
    <div>插槽信息</div></fitSteps>
           <van-button type="primary" @click="FatherClick">下一步</van-button>

  

1
2
3
4
const stepActive = ref(0)
const FatherClick = () => {
    stepActive.value += 1
}

  子组件

<van-steps :active="stepActive"  finish-icon="passed" active-icon="success">
        <van-step>测试</van-steps>
</van-steps>
<slot></slot>
const props = defineProps({
    stepActive:{
        type:Number,//类型字符串
        default:0//如果没有传递msg参数,默认值是这个
    }
})

2、父组件调用子组件的方法

父组件

<fitSteps  ref="ChildsDom">
     <div>插槽信息</div>
</fitSteps>
<van-button type="primary" @click="FatherClick">下一步</van-button>
const ChildsDom = ref(null);

const FatherClick = () => {
    
    ChildsDom.value.handelClick();
    // console.log("ChildsDom.value", ChildsDom.value)
}

 

子组件

const handelClick = () => {
    console.log('234');
}

defineExpose({ handelClick, });

 

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示