useCool 基础
使用
import { useCool } from "/@/cool";
const { refs, setRefs, mitt, router, service, upload, storage, ui } = useCool();
| 名称 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| refs | ref 的集合 | object | ||
| setRefs | 设置 ref | function(name: string) | ||
| mitt | 事件通讯,同 uni.$on | object | ||
| router | 路由管理 | Router | ||
| service | 请求服务 | Service | ||
| upload | 文件上传 | Upload | ||
| storage | 数据储存 | Storage | ||
| ui | 便捷操作 | Ui |
refs
默认绑定 ref 的方法:
<template>
<custom-el ref="customEl" />
</template>
<script lang="ts" setup>
import { ref } from "vue";
const customEl = ref<any>();
// 调用组件的方法
customEl.value.open();
</script>
使用 refs、setRefs 的方法,无需定义多个变量:
<template>
<custom-el :ref="setRefs('customEl')" />
</template>
<script lang="ts" setup>
import { useCool } from "/@/cool";
const { refs, setRefs } = useCool();
// 调用组件的方法
refs.value.customEl.open();
</script>
upload
本地上传、前端 oss 直传等
import { useCool } from "/@/cool";
const { upload } = useCool();
// 选择文件
uni.chooseImage({
count: 1,
sourceType: ["album", "camera"],
success(res) {
// 上传文件
upload(res.tempFiles[0], {
// 获取上传进度
onProgressUpdate({ progress }) {
console.log(progress);
}
}).then((url) => {
console.log(url);
});
}
});
storage
基于 uni.storage 封装:
| 名称 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| get | 根据关键字获取值 | function(key: string) | ||
| once | 获取一次后删除 | function(key: string) | ||
| set | 根据关键字设置值、过期时间 | function(key: string, value: any, expires?: number) | ||
| remove | 根据关键字删除值 | function(key: string) | ||
| info | 获取全部值 | function() | ||
| clear | 清空所有值 | function() | ||
| isExpired | 是否过期 | function(key: string) |

