Merge remote-tracking branch 'origin/base' into base

This commit is contained in:
菲鸽 2024-06-11 11:07:24 +08:00
commit 1402465fc8
3 changed files with 20 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import { UnwrapRef } from 'vue'
type IUseRequestOptions<T> = {
/** 是否立即执行如果是则在onLoad执行 */
/** 是否立即执行 */
immediate?: boolean
/** 初始化数据 */
initialData?: T
@ -11,34 +11,34 @@ type IUseRequestOptions<T> = {
* useRequest是一个定制化的请求钩子
* @param func Promise
* @param options {immediate, initialData}
* @param options.immediate true
* @param options.immediate false
* @param options.initialData undefined
* @returns {loading, error, data, run}
*/
export default function useRequest<T>(
func: () => Promise<IResData<T>>,
options: IUseRequestOptions<T> = { immediate: true },
options: IUseRequestOptions<T> = { immediate: false },
) {
const loading = ref(false)
const error = ref(false)
const data = ref<T>(options.initialData)
const run = async () => {
loading.value = true
func()
return func()
.then((res) => {
data.value = res.data as UnwrapRef<T>
error.value = false
return data.value
})
.catch((err) => {
error.value = err
throw err
})
.finally(() => {
loading.value = false
})
}
onLoad(() => {
options.immediate && run()
})
return { loading, error, data, run }
}

View File

@ -47,6 +47,7 @@ const recommendUrl = ref('http://laf.run/signup?code=ohaOgIX')
const initialData = undefined
// Service
const { loading, error, data, run } = useRequest<IFooItem>(() => getFooAPI('菲鸽'), {
immediate: true,
initialData,
})
const reset = () => {

View File

@ -62,6 +62,18 @@ export default ({ command, mode }) => {
UniManifest(),
// UniXXX 需要在 Uni 之前引入
Uni(),
{
// 临时解决 dcloudio 官方的 @dcloudio/uni-mp-compiler 出现的编译 BUG
// 参考 github issue: https://github.com/dcloudio/uni-app/issues/4952
// 自定义插件禁用 vite:vue 插件的 devToolsEnabled强制编译 vue 模板时 inline 为 true
name: 'fix-vite-plugin-vue',
configResolved(config) {
const plugin = config.plugins.find((p) => p.name === 'vite:vue')
if (plugin && plugin.api && plugin.api.options) {
plugin.api.options.devToolsEnabled = false
}
},
},
UnoCSS(),
AutoImport({
imports: ['vue', 'uni-app'],