refine: 再次简化请求

This commit is contained in:
菲鸽 2024-05-03 19:16:54 +08:00
parent 714258ca48
commit 86846486ac
3 changed files with 16 additions and 15 deletions

View File

@ -13,21 +13,20 @@
<button @click="getFoo" class="my-6">测试 GET 请求</button>
<view class="text-xl">请求数据如下</view>
<view v-if="loading" class="text-blue h-10">加载中...</view>
<view v-if="error" class="text-red h-10">请求错误</view>
<view v-else class="text-green h-10">{{ JSON.stringify(data) }}</view>
<button class="my-6" type="warn" @click="reset">一键清空数据</button>
</view>
</template>
<script lang="ts" setup>
import { getFooAPI, IFooItem } from '@/service/index/foo'
import { getFooAPI2, IFooItem } from '@/service/index/foo'
import { httpGet } from '@/utils/http'
// Service
// const { loading, data, run } = useRequest<IFooItem>(() => getFooAPI(''), { immediate: true })
const { loading, error, data, run } = useRequest<IFooItem>(() => getFooAPI2('菲鸽'))
// 便
const { loading, data, run } = useRequest<IFooItem>(() => httpGet('/foo', { name: '菲鸽' }), {
immediate: true,
})
// const { loading, error, data, run } = useRequest<IFooItem>(() => httpGet('/foo', { name: '' }))
const getFoo = () => run()
const reset = () => {

View File

@ -1,4 +0,0 @@
export interface IFooItem {
id: string
name: string
}

View File

@ -1,9 +1,10 @@
import { http } from '@/utils/http'
import type { IFooItem } from './foo.d'
import { http, httpGet } from '@/utils/http'
export interface IFooItem {
id: string
name: string
}
export { IFooItem }
/** get 请求 */
/** GET 请求 */
export const getFooAPI = (name: string) => {
return http<IFooItem>({
url: `/foo`,
@ -12,7 +13,12 @@ export const getFooAPI = (name: string) => {
})
}
/** get 请求 */
/** GET 请求 - 再次简化,看大家是否喜欢这种简化 */
export const getFooAPI2 = (name: string) => {
return httpGet<IFooItem>('/foo', { name })
}
/** POST 请求 */
export const postFooAPI = (name: string) => {
return http<IFooItem>({
url: `/foo`,