useApi
注意
使用该hooks之前,请确保按照文档方式初始化了
# 属性
useApi 用来获取api上下文对象,会返回下面几个属性:
get可发送get数据请求post可发送post数据请求put可发送put数据请求delete可发送delete数据请求request万能请求,可在上方提供的请求之外auth权限管理,一般存储用户登录的密钥
提示
如果需要auth有默认数据,可在入口处配置
# Context自定义类型
// 引入 IContext
import { IContext } from "cyl-hooks-tools"
interface IHttp {
get: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
post: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
delete: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
put: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
request: <T = any>(requestConfig?: Record<string, any>) => Promise<T>,
}
interface Auth {
id:number
}
type Context = IContext<IHttp,Auth>
import {IContext,useApi } from "cyl-hooks-tools"
interface IHttp {
get: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
post: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
delete: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
put: <T = any, P = Record<string, any>>(url: string, params?: P) => Promise<T>,
request: <T = any>(requestConfig?: Record<string, any>) => Promise<T>,
}
interface Auth {
id:number
}
type Context = IContext<IHttp,Auth>
function Component(){
const api = useApi<Context>()
console.log(api)
return <div>测试自定义useApi类型</div>
}
上次更新: 2023/05/08, 19:10:56