From f0b6e856208b8e9a705d3dcc30b8bb8b3e43f519 Mon Sep 17 00:00:00 2001 From: lihaoyuan <18278596806@163.com> Date: Tue, 28 Oct 2025 15:32:23 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BF=AE=E5=A4=8D=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=9F=90=E4=B8=AA=E7=B1=BB=E5=9E=8B=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../type/components/BaseProductTypeEdit.vue | 67 +++++++++++-------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/src/views/base/product/type/components/BaseProductTypeEdit.vue b/src/views/base/product/type/components/BaseProductTypeEdit.vue index 88bebe0..4f88fdb 100644 --- a/src/views/base/product/type/components/BaseProductTypeEdit.vue +++ b/src/views/base/product/type/components/BaseProductTypeEdit.vue @@ -660,40 +660,49 @@ }, // 规格参数默认选中 defaultSelected(specIds) { - const values = this.flatten(Object.values(this.specForm)) - const selects = values.filter((item) => - specIds.includes(item.spec_id.toString()) - ) - this.selectedSpecs = this.selectSpecs = selects - this.type_spec_names = this.selectedSpecs - .map((item) => { - return item.spec_name - }) - .join(', ') - this.form.type_spec_ids = this.selectedSpecs - .map((item) => { - return item.spec_id - }) - .join(',') + // 修复:判断 specForm 是否为有效对象 + if (!this.specForm || Object.keys(this.specForm).length === 0) { + this.selectedSpecs = this.selectSpecs = []; + this.type_spec_names = ''; + this.form.type_spec_ids = ''; + return; + } + const values = this.flatten(Object.values(this.specForm)); + const selects = values.filter((item) => + specIds.includes(item.spec_id.toString()) + ); + + this.selectedSpecs = this.selectSpecs = selects; + this.type_spec_names = selects.length > 0 + ? selects.map(item => item.spec_name).join(', ') + : ''; + this.form.type_spec_ids = selects.length > 0 + ? selects.map(item => item.spec_id).join(',') + : ''; }, // 品牌默认选中 brandDefaultSelected(brandIds) { - const values = this.flatten(Object.values(this.brandForm)) - const selects = values.filter((item) => + // 判断 brandForm 是否为有效对象,避免 Object.values 处理空值 + if (!this.brandForm || Object.keys(this.brandForm).length === 0) { + this.selectedBrands = this.selectBrands = []; + this.type_brand_names = ''; + this.form.type_brand_ids = ''; + return; + } + const values = this.flatten(Object.values(this.brandForm)); + const selects = values.filter((item) => brandIds.includes(item.brand_id.toString()) - ) - this.selectedBrands = this.selectBrands = selects - this.type_brand_names = this.selectedBrands - .map((item) => { - return item.brand_name - }) - .join(', ') - this.form.type_brand_ids = this.selectedBrands - .map((item) => { - return item.brand_id - }) - .join(',') + ); + + this.selectedBrands = this.selectBrands = selects; + // 处理无选中品牌的情况,避免 join 空数组导致 ", " + this.type_brand_names = selects.length > 0 + ? selects.map(item => item.brand_name).join(', ') + : ''; + this.form.type_brand_ids = selects.length > 0 + ? selects.map(item => item.brand_id).join(',') + : ''; }, // 扁平化数组方法 flatten(arr) {