mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2026-09-13 23:48:43 +00:00
fix(form): 避免 Select init 请求失败或路径缺失时抛出未处理异常
This commit is contained in:
parent
092bc2914d
commit
d1cb8c3853
@ -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(
|
||||
|
||||
@ -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([]);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user