收藏
回答

小程序 setData 重复执行?

在执行pickAttribute()函数时,为什么changeNumber()增加了setData函数,changeNumber()函数会自动执行,且重复执行。逻辑上这两个函数是不发生关系的。

pickAttribute: function (e) {
        if (this.data.tempOrder.count > 0 && this.data.goodsDetail.isAttribute) {
            console.log("968");
            // this.checkingAttribute();
            return
        };


        if (this.data.tempOrder.count > 0 && !this.data.goodsDetail.isAttribute) {
            console.log("974");
            this.setData({
                showAttributes: true,
            });
            return
        };


        if (this.data.tempOrder.count <= 0) {
            console.log("982");
            this.setData({
                showAttributes: !this.data.showAttributes,
            });
            return
        }
    },


changeNumber: function (e) {
        const count = e.detail.count;
        console.log("count(640) => ", count);
        const bindType = e.detail.type;
        const enter = e.detail.enter;
        const goods = (enter === 'Detail') ? this.data.goodsDetail : e.detail.goods;
        const isAttribute = goods.isAttribute;
        const inList = this.data.goodsList.findIndex(item => { return item._id === goods._id });
        if (!isAttribute) {
            this.setData({
                [`goodsList${inList}.count`]: count,
                'goodsDetail.count': count,
            });
            console.log("goodsDetail(651) => ", this.data.goodsDetail);
        };
        **//为什么加了这行代码会重复执行**
        //this.setData({
        //    'tempOrder.count': count
        //});
        console.log("tempOrder(656) => ", this.data.tempOrder);
    },


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

1 个回答

  • 陈宇明
    陈宇明
    2021-12-18

    全局搜索下 changeNumber 的调用在什么地方,从代码上来看不是在 pickAttribute 里面触发的。

    2021-12-18
    有用 2
    回复 1
    • Qiu (吉²)
      Qiu (吉²)
      2021-12-18
      找的原因了,是我在一个计数组件里加了监听函数,注释了就好了。
      2021-12-18
      回复
登录 后发表内容