EsRell_
EsRell_
  • 发布:2024-04-21 20:30
  • 更新:2024-05-07 17:53
  • 阅读:319

uniapp-x 类型推断是否存在问题?

分类:uni-app x

以下为封装的request请求泛型函数,函数为入参url及options(可为空)
HBuilder X: 4.12 (alpha)
系统: MacOS 14.2
运行环境: 安卓模拟器13

type Options = {  
    header ?: UTSJSONObject  
    data ?: any  
    timeout ?: number  
    method ?: RequestMethod  
}  

function http<T>(uri : string, op ?: Options) : Promise<T> {  
    const url = `${getApp().globalData.serviceUrl}${uri}`  
    const header = {  
        Authorization: `Bearer ${state.token}`  
    } as UTSJSONObject  

    return new Promise<T>((resolve, reject) => {  
        uni.request({  
            url,  
            data: op?.data,  
            header: header ?? null,  
            method: op?.method ?? 'GET',  
            success: res => {  
                resolve(res.data as T)  
            },  
            fail: (err) => {  
                reject(err)  
            },  
            complete: () => {  
            }  
        })  
    })  
}  

export default http

函数调用/编译运行日志报错在附件,各种类型推断错误/及参数未传

2024-04-21 20:30 负责人:DCloud_uni-ad_HDX 分享
已邀请:
1***@qq.com

1***@qq.com

不支持封装 https://doc.dcloud.net.cn/uni-app-x/api/request.html

DCloud_uni-ad_HDX

DCloud_uni-ad_HDX

  1. Android App 目前可选参数也需要传递null,示例:http<string>('', null)
  2. 第二个参数传递时需要 {} as Options
  3. 缺少返回类型,调整为 ... GetEnumOptions = (path: string) : Promise<BASE.EnumItem[]> =>  ...

要回复问题请先登录注册