uniapp封装组件及父子组件传值

1. 封装子组件 (子组件通过"props"接受父组件传的值,在子组件中定义props属性,type可以自定义类型(但是必须和父组件中的数据类型一致,否则会报错),default是默认值。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<template>
    <div class="adsList">
        <div class="cardItem" v-for="item in cardList" :key="item.index">
            <div class="cardLogo">
                <div class="adsLogo">
                    <image :src="item.icon" class="adsIcon"></image>
                    <p>{{item.name}}</p>
                </div>
                <checkbox-group @change="checkChange" v-if="cardType=='choose'">
                    <checkbox :checked="item.checked" :value="item.name" />
                </checkbox-group>
                <image src="../../static/imges/icon/write.png" class="editIcon" @click='toEdit(item)' v-if="cardType=='edit'"></image>
            </div>
            <div class="adsInfo">{{item.con?item.con:'----'}}</div>
        </div>
    </div>
</template>
 
<script>
    export default {
        props: {
            cardList: {
                type: Array,
                default () {
                    return []
                }
            },
            cardType: {
                type: String,
            },
            used:{
                type: Boolean
            }
        },
        data() {
            return {}
        },
        methods: {
            checkChange(e) {
                let val = e.detail.value;
                console.log(val, 'ppppppppp')
            },
            toEdit(item) {
                console.log(this.$props.used,'66666666666')
                uni.navigateTo({
                    url: `/pages/content/editAds/index?info=${JSON.stringify(item)}&used=${this.$props.used}`
                });
            }
        }
    }
</script>
 
<style lang="less" scoped>
 
</style>

  2. 在父组件中引入,注册,传值 ( 父组件通过“:”传值 )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<template>
    <div class="wrapper">
        <div class="adsHead">
            <div class="adsRead">
                <checkbox-group @change="checkChange">
                    <checkbox :checked="checked" />
                </checkbox-group>
                <p>Address only</p>
            </div>
            <div class="adsSearch">
                <input type="text" v-model="searchVal" @input="changeVal" class="searchInp" />
                <image src="../../../static/imges/icon/searchAds.png" class="searchIcon" />
            </div>
        </div>
        <adsCard :cardList='cardList' cardType='edit' :used='used' />
    </div>
</template>
 
<script>
    import adsCard from '../../../components/adsCard/index.vue'
    export default {
        components: {
            adsCard
        },
        data() {
            return {
                checked: false,
                used: false,
                searchVal: '',
                cardList: [{
                        icon: '../../../static/imges/icon/btg.png',
                        name: 'BTG',
                        con: '14654986535498645385',
                        checked: false
                    },
                    {
                        icon: '../../../static/imges/icon/CCL.png',
                        name: 'ETH',
                        con: '',
                        checked: false
                    },
                    {
                        icon: '../../../static/imges/icon/OKB.png',
                        name: 'BTG',
                        con: 'dfghjklkjcgfdtgfhjk',
                        checked: false
                    }
                ]
            }
        },
        onReady() {
            this.setTitle(this.$t("Users.address"))
        },
        methods: {
            changeVal() {
                this.searchVal = e.target.value
            },
            checkChange(e) {
                this.checked = !this.checked
            },
        }
    }
</script>
 
<style lang="less" scoped>
 
</style>

  

posted @   博客天天写  阅读(3049)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示