uniapp 组件传参

父组件

<v-sub @returnDate=returnDate :backGround=backGround></v-sub>

import vSub from "../../../components/v-sub.vue"
    export default {
        components:{
            vSub
        },
        data() {
            return {
                backGround:"#000"
            }
        },
        methods: {
            returnDate(e){
                console.log("接收到的值==>>" e)
            }
        }
    }

<device ref="device" :stoId="storeId" v-show="active == 'device'">
        </device>
        <battery ref="battery" :stoId="storeId" v-show="active == 'battery'">
        </battery>
        
import device from './components/device.vue'
    import battery from './components/battery.vue'
    export default {
        components: {
            device,
            battery
        },
        data() {
            return {
                storeId: null, // 参数storeId
                active: 'device' // 切换tab
            }
        },

子组件

<template>
    <view class="content" :style="[{background:backGround}]">
        <button @tap="passValue">点击传值</button>
    </view>
</template>
 
<script>
    export default {
        props:{
            backGround:{
                type:String,
                default:"#fff"
            }
        },
        methods:{
            passValue(){
                this.$emit("returnDate","yoyoyo")
            }
        }
    }
</script>
 
<style>
    .content{
        width: 400upx;
        height: 400upx;
    }
</style>

子类接收父类的数据,就会用到props

file

file

file

子组件给父组件传值首先在子类组件定义事件

file

file

file

file

父组件:file

子组件file

file

子组件

file

父组件

file

file

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。

请点赞!因为你们的赞同/鼓励是我写作的最大动力!

欢迎关注达达的简书!

这是一个有质量,有态度的博客

博客

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp 中,组件之间传递参数可以通过 props 或事件来实现。 1. 使用 props: 在父组件中,通过在子组件上定义属性来传递参数。在子组件中,可以通过 `this.$props` 或 `props` 来访问这些属性。 父组件示例: ``` <template> <div> <child :message="msg"></child> </div> </template> <script> import child from '@/components/Child.vue'; export default { components: { child }, data() { return { msg: 'Hello from parent component' } } } </script> ``` 子组件示例(Child.vue): ``` <template> <div> {{ message }} </div> </template> <script> export default { props: { message: { type: String, default: '' } } } </script> ``` 2. 使用事件: 父组件可以通过 `$emit` 方法触发一个自定义事件,并传递参数。子组件可以通过 `this.$on` 监听这个事件,并获取传递的参数。 父组件示例: ``` <template> <div> <child @customEvent="handleEvent"></child> </div> </template> <script> import child from '@/components/Child.vue'; export default { components: { child }, methods: { handleEvent(data) { console.log(data); } } } </script> ``` 子组件示例(Child.vue): ``` <template> <div @click="triggerEvent"></div> </template> <script> export default { methods: { triggerEvent() { this.$emit('customEvent', 'Hello from child component'); } } } </script> ``` 以上是传递参数的两种常用方式。根据具体需求,你可以选择适合你的场景的方式来进行组件间的参数传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值