收藏
回答

发现了一个this.setData()方法的bug

为了能够实现页面之间的通信,我写了一个通知方法

在页面1中绑定了一个changeCommentCount方法,用来修改commentSize,


changeCommentCount (newCount) {

    this.setData({

        post: {

            commentSize: newCount

        }

    })

},


在页面2中,评论成功之后通知执行该方法,传入参数修改页面1中的评论数


state.dispatch('changeCommentCount', commentSize)


修改之后,数据结构发生了奇怪的变化


数据修改前



数据修改后



也就是说,在这种情况下执行setData,当post的值为对象时,post会被整个替换掉,而不会只修改其中一个属性

回答关注问题邀请回答
收藏

4 个回答

  • 工号 9527
    工号 9527
    2016-12-20

    this.setData({

    "post.commentSize" : newCount

    })

    2016-12-20
    有用
    回复
  • 2016-12-16

    @iori 我的写法在当前页面修改是有效的

    2016-12-16
    有用
    回复
  • iori
    iori
    2016-12-16

    changeCommentCount(newCount){

        this.data.post.commentSize = newCount

       this.setData({

         post : this.data.post

       })

    }


    2016-12-16
    有用
    回复
  • iori
    iori
    2016-12-16

    应该这么写

    changeCommentCount(newCount){

        this.data.post.commentSize = newCount

       this.setData({

         post = this.data.post

       })

    }

    2016-12-16
    有用
    回复
登录 后发表内容