2.2.0 • Published 2 years ago
xluni-utils v2.2.0
xluni-utils
uniapp 框架的工具插件
安装
pnpm i xluni-utilshttp
在 utils 新建 request.ts 文件,如下
import { http } from "xluni-utils";
const defHttp = http.create({
baseUrl: "", // 接口根地址
});
// 请求拦截器 ,可以加token,返回false则中断请求
defHttp.interceptor.request = (config: any) => {
// config.header.token = "1234";
return config;
};
// 响应拦截器,返回所有的响应数据,return的值为false则会触发Promise.reject()
defHttp.interceptor.response = (response: any) => {
console.log(response);
return response;
};
export default defHttp;类型列表
- CreateConfig
| 属性 | 类型 | 默认值 | 可选值 | 备注 |
|---|---|---|---|---|
| baseUrl | string | -- | -- | 接口根地址 |
| timeout | number | 60000 | -- | 请求超时的时间,单位 ms |
| method | string | GET | POST/PUT/DELETE | 请求方式 |
| header | object | {"content-type": "application/json"} | -- | 请求头 |
| responseType | string | text | arraybuffer | 设置响应的数据类型 |
| sslVerify | boolean | true | false | 验证 ssl 证书 |
| withCredentials | boolean | false | true | 跨域请求时是否携带凭证(cookies) |
| firstIpv4 | boolean | false | true | DNS 解析时优先使用 ipv4 |
路由
toPage(url,data,events)
保留当前页面,跳转到应用内的某个页面,使用 backTo 可以返回到原页面。
url必填,需要跳转的应用内非 tabBar 的页面的路径data非必填,页面参数events非必填,页面间通信,参考地址:https://uniapp.dcloud.net.cn/api/router.html#navigateto
toTab(url)
跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
url必填,需要跳转的应用内 tabBar 的页面的路径
backTo(delta)
关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
delta非必填,返回的页面数,必须大于 0 的整数,默认为 1
closeTo(url,data)
关闭当前页面,跳转到应用内的某个页面。
url必填,需要跳转的应用内非 tabBar 的页面的路径data非必填,页面参数
closeAllTo
关闭所有页面,打开到应用内的某个页面。
url必填,需要跳转的应用内页面路径url非必填,页面参数,如果跳转的页面路径是 tabBar 页面则不能带参数
数据缓存
setStorage保存数据getStorage获取数据removeStorage删除数据
类型判断
typeOf类型判断,返回类型的小写checkType类型检查,返回布尔值isNumber数值类型isString字符类型isArray数组类型isFunction函数类型isObjectobject 类型