diff --git a/src/api/select.ts b/src/api/select.ts new file mode 100644 index 0000000..3657cd5 --- /dev/null +++ b/src/api/select.ts @@ -0,0 +1,35 @@ +// import { request } from "@/utils/service" + +/** 查 */ +export function getRemoteSelectData() { + return new Promise((resolve, reject) => { + setTimeout(() => { + // 模拟接口调用有概率出错 + if (Math.random() > 0.5) { + resolve({ + code: 0, + data: [ + { + key: 1, + label: "苹果", + value: 1 + }, + { + key: 2, + label: "香蕉", + value: 2 + }, + { + key: 3, + label: "橘子", + value: 3 + } + ], + message: "成功" + }) + } else { + reject(new Error("不小心出错了!")) + } + }, 3000) + }) +} diff --git a/src/hooks/useFetchSelect.ts b/src/hooks/useFetchSelect.ts new file mode 100644 index 0000000..d3812cf --- /dev/null +++ b/src/hooks/useFetchSelect.ts @@ -0,0 +1,60 @@ +import { onMounted, ref } from "vue" + +// 定义下拉框接收的数据格式 +export interface SelectOption { + value: string + label: string + disabled?: boolean + key?: string +} + +// 定义入参格式 +interface FetchSelectProps { + apiFun: () => Promise +} + +export function useFetchSelect(props: FetchSelectProps) { + const { apiFun } = props + + const options = ref([]) + + const loading = ref(false) + + const selectedValue = ref("") + + /* 调用接口请求数据 */ + const loadData = () => { + loading.value = true + options.value = [] + return apiFun().then( + (res) => { + loading.value = false + options.value = res.data + return res.data + }, + (err) => { + // 未知错误,可能是代码抛出的错误,或是网络错误 + loading.value = false + options.value = [ + { + value: "-1", + label: err.message, + disabled: true + } + ] + // 接着抛出错误 + return Promise.reject(err) + } + ) + } + + onMounted(() => { + loadData() + }) + + return { + options, + loading, + selectedValue + } +} diff --git a/src/icons/svg/component.svg b/src/icons/svg/component.svg new file mode 100644 index 0000000..94280f4 --- /dev/null +++ b/src/icons/svg/component.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/router/index.ts b/src/router/index.ts index 0a2b122..1aed380 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -188,6 +188,21 @@ export const constantRoutes: RouteRecordRaw[] = [ } } ] + }, + { + path: "/select", + component: Layout, + children: [ + { + path: "", + component: () => import("@/views/select/index.vue"), + name: "Select", + meta: { + title: "下拉框", + svgIcon: "component" + } + } + ] } ] diff --git a/src/views/select/index.vue b/src/views/select/index.vue new file mode 100644 index 0000000..f0234ba --- /dev/null +++ b/src/views/select/index.vue @@ -0,0 +1,16 @@ + + +