Skip to content

全局属性配置 自定义实例

本章节讲解的是如何配置全局属性(注意:仅自定义实例支持,默认实例(配置)不支持)

setRequestConfig(options:UcsDefaultRequestOptions) 设置全局属性

设置实例全局请求参数函数配置项

options 属性

  • 类型:UcsDefaultRequestOptions
参数类型描述默认值兼容性
developmentstring测试环境-请求的前缀-APP / Web / 微信小程序 / 支付宝小程序 / QQ小程序 / 快手小程序
productionstring正式环境-请求的前缀-
headerUTSJSONObject默认请求的 header-
requestTimeoutnumber请求超时时间,单位 ms60000
uploadFileTimeoutnumber上传超时时间,单位 ms120000
downloadFileTimeoutnumber下载超时时间,单位 ms120000
withCredentialsboolean跨域请求时是否携带凭证(cookies)false微信小程序 / Web
firstIpv4booleanDNS解析时优先使用ipv4false微信小程序
enableChunkedboolean开启 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)