fix: 🐛 共享下拉数据,避免重复请求
This commit is contained in:
@@ -790,7 +790,11 @@ function handleToolbar(name: string) {
|
|||||||
function handleOperate(data: IOperateData) {
|
function handleOperate(data: IOperateData) {
|
||||||
switch (data.name) {
|
switch (data.name) {
|
||||||
case "delete":
|
case "delete":
|
||||||
props.contentConfig?.deleteAction && handleDelete(data.row[pk]);
|
if (props.contentConfig?.deleteAction) {
|
||||||
|
handleDelete(data.row[pk]);
|
||||||
|
} else {
|
||||||
|
emit("operateClick", data);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
emit("operateClick", data);
|
emit("operateClick", data);
|
||||||
|
|||||||
@@ -196,12 +196,7 @@ export type IFormItems<T = IComponentType> = Array<{
|
|||||||
// 组件属性
|
// 组件属性
|
||||||
attrs?: IObject;
|
attrs?: IObject;
|
||||||
// 组件可选项(只适用于select,radio,checkbox组件)
|
// 组件可选项(只适用于select,radio,checkbox组件)
|
||||||
options?: Array<{
|
options?: Array<{ label: string; value: any; [key: string]: any }> | Ref<any[]>;
|
||||||
label: string;
|
|
||||||
value: any;
|
|
||||||
disabled?: boolean;
|
|
||||||
[key: string]: any;
|
|
||||||
}>;
|
|
||||||
// 验证规则
|
// 验证规则
|
||||||
rules?: FormItemRule[];
|
rules?: FormItemRule[];
|
||||||
// 初始值
|
// 初始值
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import UserAPI, { type UserForm } from "@/api/system/user.api";
|
import UserAPI, { type UserForm } from "@/api/system/user.api";
|
||||||
import type { IModalConfig } from "@/components/CURD/types";
|
import type { IModalConfig } from "@/components/CURD/types";
|
||||||
import DeptAPI from "@/api/system/dept.api";
|
import { deptArr, roleArr } from "./options";
|
||||||
import RoleAPI from "@/api/system/role.api";
|
|
||||||
|
|
||||||
const modalConfig: IModalConfig<UserForm> = {
|
const modalConfig: IModalConfig<UserForm> = {
|
||||||
permPrefix: "sys:user",
|
permPrefix: "sys:user",
|
||||||
@@ -51,15 +50,15 @@ const modalConfig: IModalConfig<UserForm> = {
|
|||||||
type: "tree-select",
|
type: "tree-select",
|
||||||
attrs: {
|
attrs: {
|
||||||
placeholder: "请选择所属部门",
|
placeholder: "请选择所属部门",
|
||||||
data: [],
|
data: deptArr,
|
||||||
filterable: true,
|
filterable: true,
|
||||||
"check-strictly": true,
|
"check-strictly": true,
|
||||||
"render-after-expand": false,
|
"render-after-expand": false,
|
||||||
},
|
},
|
||||||
async initFn(formItem) {
|
// async initFn(formItem) {
|
||||||
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
// // 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
||||||
formItem.attrs.data = await DeptAPI.getOptions();
|
// formItem.attrs.data = await DeptAPI.getOptions();
|
||||||
},
|
// },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "custom",
|
type: "custom",
|
||||||
@@ -77,11 +76,11 @@ const modalConfig: IModalConfig<UserForm> = {
|
|||||||
placeholder: "请选择",
|
placeholder: "请选择",
|
||||||
multiple: true,
|
multiple: true,
|
||||||
},
|
},
|
||||||
options: [],
|
options: roleArr,
|
||||||
initialValue: [],
|
initialValue: [],
|
||||||
async initFn(formItem) {
|
// async initFn(formItem) {
|
||||||
formItem.options = await RoleAPI.getOptions();
|
// formItem.options = await RoleAPI.getOptions();
|
||||||
},
|
// },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "input",
|
type: "input",
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ import UserAPI, { type UserForm } from "@/api/system/user.api";
|
|||||||
import type { IModalConfig } from "@/components/CURD/types";
|
import type { IModalConfig } from "@/components/CURD/types";
|
||||||
import { DeviceEnum } from "@/enums/settings/device.enum";
|
import { DeviceEnum } from "@/enums/settings/device.enum";
|
||||||
import { useAppStore } from "@/store";
|
import { useAppStore } from "@/store";
|
||||||
import DeptAPI from "@/api/system/dept.api";
|
import { deptArr, roleArr } from "./options";
|
||||||
import RoleAPI from "@/api/system/role.api";
|
|
||||||
|
|
||||||
const modalConfig: IModalConfig<UserForm> = {
|
const modalConfig: IModalConfig<UserForm> = {
|
||||||
permPrefix: "sys:user",
|
permPrefix: "sys:user",
|
||||||
@@ -46,15 +45,15 @@ const modalConfig: IModalConfig<UserForm> = {
|
|||||||
type: "tree-select",
|
type: "tree-select",
|
||||||
attrs: {
|
attrs: {
|
||||||
placeholder: "请选择所属部门",
|
placeholder: "请选择所属部门",
|
||||||
data: [],
|
data: deptArr, // setup,Vue会自动解包ref,不需要.value
|
||||||
filterable: true,
|
filterable: true,
|
||||||
"check-strictly": true,
|
"check-strictly": true,
|
||||||
"render-after-expand": false,
|
"render-after-expand": false,
|
||||||
},
|
},
|
||||||
async initFn(formItem) {
|
// async initFn(formItem) {
|
||||||
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
// // 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
||||||
formItem.attrs.data = await DeptAPI.getOptions();
|
// formItem.attrs.data = await DeptAPI.getOptions();
|
||||||
},
|
// },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "custom",
|
type: "custom",
|
||||||
@@ -72,11 +71,11 @@ const modalConfig: IModalConfig<UserForm> = {
|
|||||||
placeholder: "请选择",
|
placeholder: "请选择",
|
||||||
multiple: true,
|
multiple: true,
|
||||||
},
|
},
|
||||||
options: [],
|
options: roleArr,
|
||||||
initialValue: [],
|
initialValue: [],
|
||||||
async initFn(formItem) {
|
// async initFn(formItem) {
|
||||||
formItem.options = await RoleAPI.getOptions();
|
// formItem.options = await RoleAPI.getOptions();
|
||||||
},
|
// },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "input",
|
type: "input",
|
||||||
|
|||||||
31
src/views/demo/curd/config/options.ts
Normal file
31
src/views/demo/curd/config/options.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/** 公共下载数据,减少重复请求次数 */
|
||||||
|
import DeptAPI from "@/api/system/dept.api";
|
||||||
|
import RoleAPI from "@/api/system/role.api";
|
||||||
|
|
||||||
|
interface OptionType {
|
||||||
|
label: string;
|
||||||
|
value: any;
|
||||||
|
[key: string]: any; // 允许其他属性
|
||||||
|
}
|
||||||
|
|
||||||
|
// 明确指定类型为 OptionType[]
|
||||||
|
export let deptArr = ref<OptionType[]>([]);
|
||||||
|
export let roleArr = ref<OptionType[]>([]);
|
||||||
|
export let stateArr = ref<OptionType[]>([
|
||||||
|
{ label: "启用", value: 1 },
|
||||||
|
{ label: "禁用", value: 0 },
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 初始化逻辑,在 onMounted 钩子中调用
|
||||||
|
export const initOptions = async () => {
|
||||||
|
try {
|
||||||
|
// 使用Promise.all并行请求
|
||||||
|
const [dept, roles] = await Promise.all([DeptAPI.getOptions(), RoleAPI.getOptions()]);
|
||||||
|
// 获取部门选项并赋值
|
||||||
|
deptArr.value = dept;
|
||||||
|
// 获取角色选项并赋值
|
||||||
|
roleArr.value = roles;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("初始化选项失败:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import DeptAPI from "@/api/system/dept.api";
|
|
||||||
import type { ISearchConfig } from "@/components/CURD/types";
|
import type { ISearchConfig } from "@/components/CURD/types";
|
||||||
|
import { deptArr, stateArr } from "./options";
|
||||||
|
|
||||||
const searchConfig: ISearchConfig = {
|
const searchConfig: ISearchConfig = {
|
||||||
permPrefix: "sys:user",
|
permPrefix: "sys:user",
|
||||||
@@ -21,18 +21,17 @@ const searchConfig: ISearchConfig = {
|
|||||||
prop: "deptId",
|
prop: "deptId",
|
||||||
attrs: {
|
attrs: {
|
||||||
placeholder: "请选择",
|
placeholder: "请选择",
|
||||||
data: [],
|
data: deptArr,
|
||||||
filterable: true,
|
filterable: true,
|
||||||
"check-strictly": true,
|
"check-strictly": true,
|
||||||
"render-after-expand": false,
|
"render-after-expand": false,
|
||||||
clearable: true,
|
clearable: true,
|
||||||
style: { width: "200px" },
|
style: { width: "200px" },
|
||||||
},
|
},
|
||||||
async initFn(formItem) {
|
// async initFn(formItem) {
|
||||||
formItem.attrs.data = await DeptAPI.getOptions();
|
// // 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
||||||
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
// formItem.attrs.data = await DeptAPI.getOptions();
|
||||||
// this.attrs!.data = await DeptAPI.getOptions();
|
// },
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "select",
|
type: "select",
|
||||||
@@ -43,10 +42,7 @@ const searchConfig: ISearchConfig = {
|
|||||||
clearable: true,
|
clearable: true,
|
||||||
style: { width: "200px" },
|
style: { width: "200px" },
|
||||||
},
|
},
|
||||||
options: [
|
options: stateArr,
|
||||||
{ label: "启用", value: 1 },
|
|
||||||
{ label: "禁用", value: 0 },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "date-picker",
|
type: "date-picker",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { type UserForm } from "@/api/system/user.api";
|
import { type UserForm } from "@/api/system/user.api";
|
||||||
import type { IModalConfig } from "@/components/CURD/types";
|
import type { IModalConfig } from "@/components/CURD/types";
|
||||||
import DeptAPI from "@/api/system/dept.api";
|
import { deptArr } from "../config/options";
|
||||||
|
|
||||||
const modalConfig: IModalConfig<UserForm> = {
|
const modalConfig: IModalConfig<UserForm> = {
|
||||||
colon: true,
|
colon: true,
|
||||||
@@ -35,15 +35,11 @@ const modalConfig: IModalConfig<UserForm> = {
|
|||||||
type: "tree-select",
|
type: "tree-select",
|
||||||
attrs: {
|
attrs: {
|
||||||
placeholder: "请选择",
|
placeholder: "请选择",
|
||||||
data: [],
|
data: deptArr,
|
||||||
filterable: true,
|
filterable: true,
|
||||||
"check-strictly": true,
|
"check-strictly": true,
|
||||||
"render-after-expand": false,
|
"render-after-expand": false,
|
||||||
},
|
},
|
||||||
async initFn(formItem) {
|
|
||||||
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
|
||||||
formItem.attrs.data = await DeptAPI.getOptions();
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "custom",
|
type: "custom",
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
import DeptAPI from "@/api/system/dept.api";
|
|
||||||
import type { ISearchConfig } from "@/components/CURD/types";
|
import type { ISearchConfig } from "@/components/CURD/types";
|
||||||
|
import { deptArr, stateArr } from "../config/options";
|
||||||
const selectOptions = reactive([
|
|
||||||
{ label: "启用", value: 1 },
|
|
||||||
{ label: "禁用", value: 0 },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const searchConfig: ISearchConfig = {
|
const searchConfig: ISearchConfig = {
|
||||||
grid: "right",
|
grid: "right",
|
||||||
@@ -38,7 +33,7 @@ const searchConfig: ISearchConfig = {
|
|||||||
label: "下拉选择框",
|
label: "下拉选择框",
|
||||||
prop: "testSelect",
|
prop: "testSelect",
|
||||||
attrs: { placeholder: "全部", clearable: true },
|
attrs: { placeholder: "全部", clearable: true },
|
||||||
options: selectOptions,
|
options: stateArr as any,
|
||||||
events: {
|
events: {
|
||||||
change: function (e) {
|
change: function (e) {
|
||||||
console.log("选中的值: ", e);
|
console.log("选中的值: ", e);
|
||||||
@@ -51,17 +46,16 @@ const searchConfig: ISearchConfig = {
|
|||||||
prop: "testTreeSelect",
|
prop: "testTreeSelect",
|
||||||
attrs: {
|
attrs: {
|
||||||
placeholder: "请选择",
|
placeholder: "请选择",
|
||||||
data: [],
|
data: deptArr,
|
||||||
filterable: true,
|
filterable: true,
|
||||||
"check-strictly": true,
|
"check-strictly": true,
|
||||||
"render-after-expand": false,
|
"render-after-expand": false,
|
||||||
clearable: true,
|
clearable: true,
|
||||||
},
|
},
|
||||||
async initFn(formItem) {
|
// async initFn(formItem) {
|
||||||
formItem.attrs.data = await DeptAPI.getOptions();
|
// // 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
||||||
// 注意:如果initFn函数不是箭头函数,this会指向此配置项对象,那么也就可以用this来替代形参formItem
|
// formItem.attrs.data = await DeptAPI.getOptions();
|
||||||
// this.attrs!.data = await DeptAPI.getOptions();
|
// },
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "cascader",
|
type: "cascader",
|
||||||
@@ -117,13 +117,15 @@ import UserAPI from "@/api/system/user.api";
|
|||||||
import type { IObject, IOperateData, PageModalInstance } from "@/components/CURD/types";
|
import type { IObject, IOperateData, PageModalInstance } from "@/components/CURD/types";
|
||||||
import usePage from "@/components/CURD/usePage";
|
import usePage from "@/components/CURD/usePage";
|
||||||
import addModalConfig from "./config/add";
|
import addModalConfig from "./config/add";
|
||||||
import addModalConfig2 from "./config/add2";
|
|
||||||
import contentConfig from "./config/content";
|
import contentConfig from "./config/content";
|
||||||
import contentConfig2 from "./config/content2";
|
|
||||||
import editModalConfig from "./config/edit";
|
import editModalConfig from "./config/edit";
|
||||||
import editModalConfig2 from "./config/edit2";
|
|
||||||
import searchConfig from "./config/search";
|
import searchConfig from "./config/search";
|
||||||
import searchConfig2 from "./config/search2";
|
import { initOptions } from "./config/options";
|
||||||
|
|
||||||
|
import addModalConfig2 from "./config2/add";
|
||||||
|
import contentConfig2 from "./config2/content";
|
||||||
|
import editModalConfig2 from "./config2/edit";
|
||||||
|
import searchConfig2 from "./config2/search";
|
||||||
|
|
||||||
const {
|
const {
|
||||||
searchRef,
|
searchRef,
|
||||||
@@ -141,8 +143,6 @@ const {
|
|||||||
handleFilterChange,
|
handleFilterChange,
|
||||||
} = usePage();
|
} = usePage();
|
||||||
|
|
||||||
const addModalRef2 = ref();
|
|
||||||
|
|
||||||
// 其他工具栏
|
// 其他工具栏
|
||||||
function handleToolbarClick(name: string) {
|
function handleToolbarClick(name: string) {
|
||||||
console.log(name);
|
console.log(name);
|
||||||
@@ -195,6 +195,7 @@ const handleOperateClick2 = (data: IOperateData) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 打开二级弹窗
|
// 打开二级弹窗
|
||||||
|
const addModalRef2 = ref();
|
||||||
const openSecondModal = () => {
|
const openSecondModal = () => {
|
||||||
handleAddClick(addModalRef2 as Ref<PageModalInstance>);
|
handleAddClick(addModalRef2 as Ref<PageModalInstance>);
|
||||||
};
|
};
|
||||||
@@ -205,4 +206,8 @@ const secondSubmit = (formData: any) => {
|
|||||||
|
|
||||||
// 切换示例
|
// 切换示例
|
||||||
const isA = ref(true);
|
const isA = ref(true);
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
initOptions();
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user