名字是乱打的
名字是乱打的
  • 发布:2022-10-10 23:07
  • 更新:2022-10-11 09:36
  • 阅读:288

【报Bug】父组件有个map ,value是个对象数组,子组件绑定对象数组中的对象,父组件数据变化传达到子组件,但是子组件没有根据对象变化重新渲染

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号: 12.6

HBuilderX类型: 正式

HBuilderX版本号: 3.6.4

手机系统: iOS

手机系统版本号: iOS 14

手机厂商: 模拟器

手机机型: iphone 12

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

i父组件 ndex.vue

<template>  
    <view>  
        <scroll-view scroll-x class="scroll-row border-bottom" :scroll-into-view="scrollIntoView" scroll-with-animation style="height: 75rpx;">  
            <view  
                v-for="(item, index) in tabBars"  
                :key="index"  
                :id="'tab' + index"  
                class="scroll-row-item  px-3 py-1 font-sm"  
                :class="currSelectBar == index ? 'select-color-little font-weight-bolder font ' : ''"  
                @click="changeBar(index)"  
            >  
                {{ item.name }}  
            </view>  
        </scroll-view>  

        <swiper :current="currSelectBar" @change="swiperBar" :style="'height:' + infoListHeight + 'px'">  
            <swiper-item v-for="(item, index) in tabBars" :key="index">  
                <view class="swiper-item">  
                    <scroll-view scroll-y="true" style="height: 100%;" :style="'height:' + infoListHeight + 'px'">  
                        <!-- 列表 -->  
                        <block v-for="(item2, index2) in tabInfoData.get(index)" :key="index2">  
                            <info-list-item :item="item2" :index="index2"  @followOhter="followOhter" @infoLike="infoLike"></info-list-item>  
                            <divider />  
                        </block>  
                    </scroll-view>  
                </view>  
            </swiper-item>  
        </swiper>  

    </view>  
</template>  

<script>  
import infoListItem from '@/components/common/info-list-item.vue';  
export default {  
    components: {  
        infoListItem  
    },  
    data() {  
        return {  
            //列表高度  
            infoListHeight: null,  
            // 顶部选项卡选项数据  
            tabBars: [  
                { name: '关注', id: 1 },  
                { name: '推荐', id: 2 },  
                { name: '软件工程', id: 3 },  
                { name: '物联网', id: 4 },  
                { name: '财务管理', id: 5 },  
                { name: '新能源车', id: 6 },  
                { name: '园林', id: 7 },  
                { name: '法律', id: 8 }  
            ],  
            scrollIntoView: 'tab1',  
            currSelectBar: 1,  
            // 每个选项卡下的资讯列表数据  
            tabInfoData: new Map()  
        };  
    },  
    onLoad() {  
        //计算列表也高度  
        uni.getSystemInfo({  
            success: res => {  
                this.infoListHeight = res.windowHeight - uni.upx2px(75 + 1);  
            }  
        });  

        //获取列表页数据  
        this.getData();  
    },  
    methods: {  
        getData() {  
            this.tabBars.forEach((item, index) => {  
                this.tabInfoData.set(index, [  
                    {  
                        userName: '小明' + index,  
                        pushDate: '2022-9-26 晚上23:47',  
                        userPic: '../../static/default.jpg',  
                        infoContent:  
                            '“重阳节又叫登高节、重九节、菊花节等等,寓意平安、健康长寿。对于重阳节节日名称的由来,程鹏说,在《易经》中,“九”数为阳数,“九九”两阳数相重,所以称作“重阳”;因日与月皆逢九,故又称为“重九”。”',  
                        infoPreviewPic: '../../static/demo/banner2.jpg',  
                        hasLike: false,  
                        hasFollow: true,  
                        likeCount: 999,  
                        commentCount: 88,  
                        shareCount: 77  
                    },  
                    {  
                        userName: '小明2',  
                        pushDate: '2022-9-26 晚上23:47',  
                        userPic: '../../static/default.jpg',  
                        infoContent:  
                            '“重阳节又叫登高节、重九节、菊花节等等,寓意平安、健康长寿。对于重阳节节日名称的由来,程鹏说,在《易经》中,“九”数为阳数,“九九”两阳数相重,所以称作“重阳”;因日与月皆逢九,故又称为“重九”。”',  
                        infoPreviewPic: '',  
                        hasFollow: false,  
                        hasLike: true,  
                        likeCount: 888,  
                        commentCount: 66,  
                        shareCount: 55  
                    },  
                    {  
                        userName: '小明3',  
                        pushDate: '2022-9-26 晚上23:47',  
                        userPic: '../../static/default.jpg',  
                        infoContent:  
                            '“重阳节又叫登高节、重九节、菊花节等等,寓意平安、健康长寿。对于重阳节节日名称的由来,程鹏说,在《易经》中,“九”数为阳数,“九九”两阳数相重,所以称作“重阳”;因日与月皆逢九,故又称为“重九”。”',  
                        hasFollow: true,  
                        hasLike: false,  
                        likeCount: 444,  
                        commentCount: 33,  
                        shareCount: 22  
                    }  
                ]);  
            });  
        },  

        //监听swiperBar滑动  
        swiperBar(e) {  
            this.changeBar(e.detail.current);  
        },  

        //关注  
        followOhter: function(index) {  
            this.tabInfoData.get(this.currSelectBar)[index].hasFollow = true;  

            uni.showToast({  
                title: '关注成功,'  
            });  
        },  

        //关注  
        // followOhter: function(index) {  
        //  this.tabInfoData.get(this.currSelectBar)[index].hasFollow = true;  
        //  let tempStr=JSON.stringify(this.tabInfoData)  
        //  this.tabInfoData=new Map  
        //  this.tabInfoData=JSON.parse(tempStr)  

        //  uni.showToast({  
        //      title: '关注成功'  
        //  });  
        // },  

        //点赞或者取消点赞  
        infoLike: function(index) {  
            //旧值取反,得到现在是点赞还是取消点赞  
            var hasLikeCurr = this.tabInfoData.get(this.currSelectBar)[index].hasLike == true ? false : true;  
            this.tabInfoData.get(this.currSelectBar)[index].hasLike = hasLikeCurr;  
            if (hasLikeCurr) {  
                this.tabInfoData.get(this.currSelectBar)[index].likeCount += 1;  
            } else {  
                this.tabInfoData.get(this.currSelectBar)[index].likeCount -= 1;  
            }  
        },  

        //切换选项卡  
        changeBar: function(index) {  
            this.currSelectBar = index;  
            this.scrollIntoView = 'tab' + index;  
        }  
    }  
};  
</script>  

<style></style>

子组件

<template>  
    <view class="p-2">  
        <!-- 头像 -->  
        <view class="flex align-center justify-between">  
            <view class="flex align-center justify-start">  
                <image class="border-radius-100" :src="item.userPic" style="height: 65rpx; width: 65rpx;" lazy-load @click.stop="openMy"></image>  
                <view class="flex-column justify-between ml-2">  
                    <view class="font" style=" line-height: 1.5;" @click.stop="openMy">{{ item.userName }}</view>  
                    <view class="font-small text-gray">{{ item.pushDate }}</view>  
                </view>  
            </view>  
            <view  
                class="flex align-center justify-center rounded bu-bg-color text-white font animated faster"  
                style="width: 90rpx; height: 50rpx;"  
                hover-class="rubberBand"  
                @click="followOhter"  
                v-if="item.hasFollow != true"  
            >  
                关注  
            </view>  
        </view>  

        <view @click="openInfoDetail">  
            <!-- 标题+略所文 -->  
            <view class="font">{{ item.infoContent }}</view>  
            <!-- 图片 -->  
            <image class="rounded w-100" v-show="item.infoPreviewPic" :src="item.infoPreviewPic" style="height: 350rpx;"></image>  
        </view>  

        <!-- 功能栏 -->  
        <view class="flex align-center">  
            <view class="flex justify-center align-center flex-1" hover-class="select-color-little" @click="infoShare">  
                <text class="iconfont icon-fenxiang mr-10"></text>  
                <text>{{ item.shareCount ? item.shareCount : '分享' }}</text>  
            </view>  

            <view class="flex justify-center align-center flex-1" hover-class="select-color-little" @click="openCommentList">  
                <text class="iconfont icon-pinglun2 mr-10"></text>  
                <text>{{ item.commentCount ? item.commentCount : '评论' }}</text>  
            </view>  

            <view class="flex justify-center align-center flex-1" :class="item.hasLike == true ? 'select-color-little' : ''" @click="infoLike">  
                <text class="iconfont icon-dianzan2 mr-10"></text>  
                <text>{{ item.likeCount ? item.likeCount : '点赞' }}</text>  
            </view>  
        </view>  
    </view>  
</template>  

<script>  
export default {  
    props: {  
        item: Object,  
        index: Number  
    },  
    methods: {  
        //打开个人空间  
        openMy: function() {  
            console.log('点击头像进入个人空间');  
        },  
        followOhter: function() {  
            console.log('关注这个人哦');  
            console.log(this.item);  
            console.log(this.item.hasFollow != true);  
            //通知父组件  
            this.$emit('followOhter', this.index);  
        },  
        infoLike: function() {  
            console.log('点赞');  
            this.$emit('infoLike', this.index);  
        },  
        openCommentList: function() {  
            console.log('打开评论列表');  
        },  
        infoShare: function() {  
            console.log('分享该资讯');  
        },  
        openInfoDetail: function() {  
            console.log('打开资讯详情');  
        }  
    }  
};  
</script>  

<style></style>  

操作步骤:

点击关注或者点赞,数据已经变化,但是子组件没有重新渲染

预期结果:

点击关注或者点赞,子组件重新渲染,比如点了关注,关注按钮要隐藏

实际结果:

点了关注关注按钮没有隐藏,点赞状态也没有变化

bug描述:

子组件数据变化了 却没有渲染,watch也监听不到数据变化,但是从打印结果来看数据缺失变化了

具体情况:
首页中有资讯列表,列表由一个个单个资讯构成,单个资讯是一个组件,数据由父传到子,我点击关注和点赞触发子组件函数,子组件emit传达到父组件,父组件改变数据,传达到子组件,子组件数据变化了却没有渲染

2022-10-10 23:07 负责人:无 分享
已邀请:
zZZ1Ma

zZZ1Ma

v-for="(item2, index2) in tabInfoData.get(index)" :key="index2" 用计算属性或方法返回tabInfoData.get(index)用于循环试试

该问题目前已经被锁定, 无法添加新回复