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) {