全局属性配置 自定义实例
本章节讲解的是如何配置全局属性(注意:仅自定义实例支持,默认实例(配置)不支持)
setRequestConfig(options:UcsDefaultRequestOptions) 设置全局属性
设置实例全局请求参数函数配置项
options 属性
- 类型:
UcsDefaultRequestOptions
| 参数 | 类型 | 描述 | 默认值 | 兼容性 |
|---|---|---|---|---|
| development | string | 测试环境-请求的前缀 | - | APP / Web / 微信小程序 / 支付宝小程序 / QQ小程序 / 快手小程序 |
| production | string | 正式环境-请求的前缀 | - | |
| header | UTSJSONObject | 默认请求的 header | - | |
| requestTimeout | number | 请求超时时间,单位 ms | 60000 | |
| uploadFileTimeout | number | 上传超时时间,单位 ms | 120000 | |
| downloadFileTimeout | number | 下载超时时间,单位 ms | 120000 | |
| withCredentials | boolean | 跨域请求时是否携带凭证(cookies) | false | 微信小程序 / Web |
| firstIpv4 | boolean | DNS解析时优先使用ipv4 | false | 微信小程序 |
| enableChunked | boolean | 开启 transfer-encoding chunked。 | false |
示例
参考上章节所创建的自定义请求实例 request,这里使用上章节创建的自定义实例作为演示
ts
request.setRequestConfig({
// 测试环境
development: "http://xxxxx",
// 正式环境
production: "",
header: {
"custom-header" : "hello"
}
});getRequestConfig 获取全局属性配置
获取 setRequestConfig API设置的全局属性配置
ts
const requestConfig = request.getRequestConfig();
// 输出 setRequestConfig API 所设置的全局属性
console.log(requestConfig)