loading状态控制相关
useAutoRequest
针对单个接口的loading状态
多个按钮使用同一个状态,第二个参数可选
<script setup lang="ts">
import { message } from 'ant-design-vue'
import { useAutoRequest } from '@hooks/components'
const submitFetch = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('执行成功')
}, 1500)
})
}
const [loading, submit] = useAutoRequest(submitFetch, {
onSuccess: (res) => {
message.success(res)
}
})
</script>
<template>
<div>
<a-space>
<!-- 多个按钮使用同一个loading状态 -->
<a-button :loading="loading">取消</a-button>
<a-button :loading="loading" type="primary" @click="submit">提交</a-button>
</a-space>
</div>
</template>
<style lang="less" scoped></style>
<script setup lang="ts">
import { message } from 'ant-design-vue'
import { useAutoRequest } from '@hooks/components'
const submitFetch = () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve('执行成功')
}, 1500)
})
}
const [loading, submit] = useAutoRequest(submitFetch, {
onSuccess: (res) => {
message.success(res)
}
})
</script>
<template>
<div>
<a-space>
<!-- 多个按钮使用同一个loading状态 -->
<a-button :loading="loading">取消</a-button>
<a-button :loading="loading" type="primary" @click="submit">提交</a-button>
</a-space>
</div>
</template>
<style lang="less" scoped></style>
参数
参数 | 描述 | 必选 | 类型 | 默认值 |
---|---|---|---|---|
fun | 调用远程接口返回数据 | true | ()=>Promise | - |
options | 调用时的附加选项 | false | 见下文 | - |
options
参数 | 描述 | 必选 | 类型 | 默认值 |
---|---|---|---|---|
loading | 初始loading状态 | false | boolean | false |
onSuccess | 成功时的回调 | false | (data: any) => void | - |
返回值
参数 | 描述 | 类型 |
---|---|---|
requestLoading | loading状态 | boolean |
run | 调用函数,参数与入参保持一致 | ()=>Promise |
useAutoLoading
针对多个接口的loading状态
多个按钮使用同一个状态,第二个参数可选
<script setup lang="ts">
import { message } from 'ant-design-vue'
import { useAutoLoading } from '@hooks/components'
const submitFetch = (type: number) => {
return new Promise((resolve) => {
console.log('submitFetch', type)
setTimeout(() => {
resolve('执行成功')
}, 1500)
})
}
const saveFetch = (type: number) => {
return new Promise((resolve) => {
console.log('saveFetch', type)
setTimeout(() => {
resolve('执行成功')
}, 1500)
})
}
const [loading, run] = useAutoLoading()
function submit(type: number) {
if (type === 1) {
run(submitFetch(type)).then((res) => {
message.success(res)
})
} else {
run(saveFetch(type)).then((res) => {
message.success(res)
})
}
}
</script>
<template>
<div>
<a-space>
<!-- 多个按钮使用同一个loading状态 -->
<a-button :loading="loading" @click="submit(2)">暂存</a-button>
<a-button :loading="loading" type="primary" @click="submit(1)">提交</a-button>
</a-space>
</div>
</template>
<style lang="less" scoped></style>
<script setup lang="ts">
import { message } from 'ant-design-vue'
import { useAutoLoading } from '@hooks/components'
const submitFetch = (type: number) => {
return new Promise((resolve) => {
console.log('submitFetch', type)
setTimeout(() => {
resolve('执行成功')
}, 1500)
})
}
const saveFetch = (type: number) => {
return new Promise((resolve) => {
console.log('saveFetch', type)
setTimeout(() => {
resolve('执行成功')
}, 1500)
})
}
const [loading, run] = useAutoLoading()
function submit(type: number) {
if (type === 1) {
run(submitFetch(type)).then((res) => {
message.success(res)
})
} else {
run(saveFetch(type)).then((res) => {
message.success(res)
})
}
}
</script>
<template>
<div>
<a-space>
<!-- 多个按钮使用同一个loading状态 -->
<a-button :loading="loading" @click="submit(2)">暂存</a-button>
<a-button :loading="loading" type="primary" @click="submit(1)">提交</a-button>
</a-space>
</div>
</template>
<style lang="less" scoped></style>
参数
参数 | 描述 | 必选 | 类型 | 默认值 |
---|---|---|---|---|
defaultLoading | 初始loading状态 | false | boolean | false |
返回值
参数 | 描述 | 类型 |
---|---|---|
requestLoading | loading状态 | boolean |
run | 包裹函数,参数为一个promise,回参也是promise,函数内部会监听入参promise的状态,并在适当时机更改loading状态 | (promise:Promise)=>Promise |