diff --git a/components/ProductWindow.vue b/components/ProductWindow.vue index 4d7f731..acee08f 100644 --- a/components/ProductWindow.vue +++ b/components/ProductWindow.vue @@ -66,18 +66,16 @@ export default { type: Object, default: () => {} }, - productValue: { - type: Object, - default: () => {} + // 使用字符串再转成对象的原因是,当key='2.5g'时,会变成key='2',导致数据丢失 + productValueStr: { + type: String, + default: '{}' }, cartNum: { type: Number, default: () => 1 } }, - data: function() { - return {}; - }, methods: { previewImg:function(imgUrl){ uni.previewImage({ @@ -85,50 +83,49 @@ export default { }) }, closeAttr: function() { - this.$emit("changeFun", { action: "changeattr", value: false }); + this.$emit("changeFun", { action: "changeattr", value: false }) }, CartNumDes: function() { - this.$emit("changeFun", { action: "ChangeCartNum", value: false }); + this.$emit("changeFun", { action: "ChangeCartNum", value: false }) }, CartNumAdd: function() { - this.$emit("changeFun", { action: "ChangeCartNum", value: 1 }); + this.$emit("changeFun", { action: "ChangeCartNum", value: 1 }) }, tapAttr: function(indexw, indexn, itemn) { - if (this.productValue[itemn.attr].stock === 0) { - return + const productValue = JSON.parse(this.productValueStr || '{}') + if (productValue[itemn.attr]) { + if (productValue[itemn.attr].stock === 0) { + return + } } // 修改商品规格不生效的原因: // H5端下面写法,attr更新,但是除H5外其他端不支持, // 尽量避免下面的骚写法,不要在子组件内更新props // 这里修改是为了能获取到被选中的属性 - this.attr.productAttr[indexw].index = indexn; - let that = this; - let value = that - .getCheckedValue() - .sort() - .join(","); - that.$emit("changeFun", { + this.attr.productAttr[indexw].index = indexn + const value = this.getCheckedValue().sort().join(",") + this.$emit("changeFun", { action: "ChangeAttr", value: { value, indexw, indexn } - }); + }) }, - //获取被选中属性; + // 获取被选中属性 getCheckedValue: function() { - let productAttr = this.attr.productAttr; - let value = []; + const productAttr = this.attr.productAttr + const value = [] for (let i = 0; i < productAttr.length; i++) { for (let j = 0; j < productAttr[i].attrValueArr.length; j++) { if (productAttr[i].index === j) { - value.push(productAttr[i].attrValueArr[j]); + value.push(productAttr[i].attrValueArr[j]) } } } - return value; + return value } } -}; +} diff --git a/pages/shop/GoodsCon/index.vue b/pages/shop/GoodsCon/index.vue index 4656fe4..d68156a 100644 --- a/pages/shop/GoodsCon/index.vue +++ b/pages/shop/GoodsCon/index.vue @@ -475,7 +475,7 @@