2***@qq.com
2***@qq.com
  • 发布:2021-03-04 18:14
  • 更新:2022-08-30 00:12
  • 阅读:2500

求问为何生命周期computed函数只在页面首次加载时生效,data函数改变computed函数里的值也不执行

分类:uni-app
<text class="price fill" v-if="config.deletes == false">  
                <text class="sml">合计:</text>  
                ¥{{ totalPrice }}  
            </text>  
export default {  
          data() {  
            return {  
                config: {  
                    deletes: false,  
                    swipeAction: false,     // 是否禁止滑动  
                    showModal: false,       // 是否开启删除询问  
                    checked: false,         //是否全选  
                    button: {  
                        customStyle: {  
                            color: '#fff',  
                            backgroundColor: '#1783F9',  
                            margin: '0',  
                            padding: '0 40rpx',  
                            width: '200rpx',  
                            fontSize: '30rpx'  
                        },  
                        customStyle2: {  
                            color: '#1783F9',  
                            backgroundColor: 'rgba(0,0,0, 0)',  
                            marginTop: '0',  
                            marginBottom: '0',  
                            marginRight: '20rpx',  
                            marginLeft: '0',  
                            padding: '0 40rpx',  
                            border: 'none',  
                            width: '200rpx',  
                            fontSize: '28rpx'  
                        },  
                        customStyle3: {  
                            color: '#DA1619',  
                            backgroundColor: 'rgba(0,0,0, 0)',  
                            margin: '0',  
                            padding: '0 40rpx',  
                            border: 'none',  
                            width: '200rpx',  
                            fontSize: '28rpx'  
                        }  
                    },  
                    modal: {  
                        title: '提示',  
                        content: '确认删除商品?',  
                        titleStyle: 'font-size: 36upx;',  
                        contentStyle: {  
                            fontSize: '36upx'  
                        },  
                        confirmStyle: {  
                            color: '#fff',  
                            backgroundColor: '#FD013E'  
                        }  
                    },  
                    options: [  
                        {  
                            text: '删除',  
                            style: {  
                                backgroundColor: '#FD013E'  
                            }  
                        }  
                    ],  
                    page: {  
                        current: 1,  
                        size: 10  
                    }  
                },  
                data: {  
                    cartList: [],  
                    delId: ''  
                }  
            }  
        },  
                computed: {  
            // 价格合计  
            totalPrice() {  
                let sumPrice = 0,  
                    items = this.data.cartList;  
                items.forEach(val => {  
                    let priceVal = parseFloat(val.price),  
                        salesVal = parseFloat(val.number);  
                    if (val.check) {  
                        sumPrice += priceVal * salesVal;  
                    }  
                });  
                // sumPrice = sumPrice == 0 ? 0 : sumPrice.toFixed(2);  
                let tofixNum = sumPrice.toFixed(2);  
                return parseFloat(tofixNum);  
            },  
            // 数量统计  
            totalCount() {  
                let totalNumber = 0,  
                    items = this.data.cartList;  
                items.forEach(val => {  
                    if (val.check) {  
                        totalNumber += val.number  
                    }  
                });  
                return totalNumber  
            },  
            // 价格整数  
            priceInt() {  
                return val => {  
                    let priceStr = val.toString();  
                    if (priceStr != parseInt(priceStr)) {  
                        return priceStr.split('.')[0];  
                    } else {  
                        return priceStr;  
                    }  
                };  
            },  
            // 价格小数  
            priceDecimal() {  
                return val => {  
                    let priceStr = val.toString();  
                    if (priceStr != parseInt(priceStr)) {  
                        return priceStr.split('.')[1];  
                    } else {  
                        return '00';  
                    }  
                };  
            },  
            // 用户信息  
            userInfo() {  
                let userInfo = {}  
                if ( this.$store.state.userInfo ) {  
                    userInfo = JSON.parse(this.$store.state.userInfo)  
                }  
                return userInfo  
            }  
        },  

}
2021-03-04 18:14 负责人:无 分享
已邀请:
勇哥萌萌哒

勇哥萌萌哒

computed无法监听数组,用this.$set 设置数组数据,watch监听checkbox开关,然后this.$set(arr,index,value)

dashuai

dashuai

我也遇到了这个情况,而且在微信小程序里是首次可用,再重新打开页面,computed的就不执行了

苹果和安卓原生是可以正常使用 computed 计算属性的

  • dashuai

    不是 数组或对象 的成员不能监听的 问题,否则,苹果和安卓原生app是不能用的。

    2022-08-30 00:13

要回复问题请先登录注册