解决uni-app props 传递数组修改后不能使用问题

1.子组件页面结构

//NoticesMarquee 组件
<view v-for="(item, index) in tempList" :key="index" >
        {{item.Title}}
</view>

2.父组件中使用

在父组件中引用子组件并传递值。

<template>
    <view>
           <!--使用子组件  -->
        <notices-marquee  :items="noticesList" ></notices-marquee>
    </view>
</template>

<script>
        import NoticesMarquee from '@/components/index/NoticesMarquee'
    export default {
        components:{
            NoticesMarquee
        },
        data() {
            return {
                noticesList: [{
                        Title: '4874545454554545454545454',
                        Id: 2
                    },
                    {
                        Title: '7888844844456454564564565465656',
                        Id: 1
                    },
                    {
                        Title: '78947898526665656+56+5+656',
                        Id: 3
                    },
                ],
            };
        }
    }
</script>
<style>

</style>

3.问题描述

3.1 问题概述:

现象为:在setTimeout()中修改值,但是对 items这个数组并不起作用,即修改后的数组与原来一致,并没有达到修改数组的效果,代码如下:

export default {
        props: ['items'],
        methods: {
        showMarquee: function() {
                let _this = this;
                setTimeout(function() {
                    _this.items.push(_this.items[0]);
                    _this.items.shift()
                }, 500)
            },
        },
    }

3.1 解决办法:

使用中间临时数组(tempList(),在created()时就开始将值传递给临时数组,防止出现延时情况,后续单独操作临时数组即可。

export default {
        props: ['items'],
        data() {
            return {
                tempList: []
            }
        },
        methods: {
            showMarquee: function() {
                let _this = this;
                setTimeout(function() {
                    _this.tempList.push(_this.tempList[0]);
                    _this.tempList.shift()
                }, 500)
            },
        },
        created() {
            this.tempList = this.items
        }
    }

 

推荐是最好的支持,关注是最大的鼓励。亲爱的朋友,很荣幸在园子里遇到您,希望能与您一起学习~~~。

posted on 2019-04-17 11:33 WFaceBoss 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wfaceboss/p/10722458.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值