From d1cb8c3853ac724b424dbe8de2d2a63bf6302324 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Fri, 4 Sep 2026 11:27:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(form):=20=E9=81=BF=E5=85=8D=20Select=20init?= =?UTF-8?q?=20=E8=AF=B7=E6=B1=82=E5=A4=B1=E8=B4=A5=E6=88=96=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E7=BC=BA=E5=A4=B1=E6=97=B6=E6=8A=9B=E5=87=BA=E6=9C=AA?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/form/src/fields/Select.vue | 15 ++++++++++++--- packages/form/tests/unit/fields/Select.spec.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/form/src/fields/Select.vue b/packages/form/src/fields/Select.vue index 9a9c447f..238853c2 100644 --- a/packages/form/src/fields/Select.vue +++ b/packages/form/src/fields/Select.vue @@ -372,7 +372,12 @@ const getInitOption = async () => { }); } - let initData = getValueByKeyPath(initRoot || root, res); + let initData: any; + try { + initData = getValueByKeyPath(initRoot || root, res); + } catch { + // 响应中不存在 initRoot/root 路径时按无数据处理 + } if (initData) { if (!Array.isArray(initData)) { initData = [initData]; @@ -417,8 +422,12 @@ if (typeof props.config.options === 'function') { const v = props.model[props.name]; if (Array.isArray(v) ? !v.length : typeof v === 'undefined') return; if (typeof v === 'undefined' || hasOption(v)) return; - const data = await getInitOption(); - setOptions(data); + try { + const data = await getInitOption(); + setOptions(data); + } catch { + setOptions([]); + } }; watch( diff --git a/packages/form/tests/unit/fields/Select.spec.ts b/packages/form/tests/unit/fields/Select.spec.ts index 4053f2f1..0d2a1ac1 100644 --- a/packages/form/tests/unit/fields/Select.spec.ts +++ b/packages/form/tests/unit/fields/Select.spec.ts @@ -691,4 +691,16 @@ describe('Select - getInitOption 初始化接口分支', () => { expect((wrapper.findComponent(MSelect).vm as any).options).toEqual([]); }); + + test('init 请求失败时 options 为空且不抛未处理异常', async () => { + request = vi.fn(async () => { + throw new Error('init-fail'); + }); + setConfig({ request }); + + const wrapper = mountFormWithRequest({ initUrl: 'https://example.com/init', initRoot: 'data.obj' }, { s: 'a' }); + await flushAsync(); + + expect((wrapper.findComponent(MSelect).vm as any).options).toEqual([]); + }); });