发现了一个this.setData()方法的bug
发布于 6 年前 作者 na51 7954 次浏览 来自 问答

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

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

changeCommentCount (newCount) {

    this.setData({

        post: {

            commentSize: newCount

        }

    })

},

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

state.dispatch('changeCommentCount', commentSize)

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

数据修改前

数据修改后

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

4 回复

this.setData({

“post.commentSize” : newCount

})

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

应该这么写

changeCommentCount(newCount){

    this.data.post.commentSize = newCount

   this.setData({

     post = this.data.post

   })

}

changeCommentCount(newCount){

    this.data.post.commentSize = newCount

   this.setData({

     post : this.data.post

   })

}

回到顶部