数据缓存(存储在localstorage中)
异步
uni.setStorage:将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
// 异步 有相关回调
uni.setStorage({
key: 'my-Key-one',
data: '第一个Key',
});
uni.getStorage: 从本地缓存中异步获取指定 key 对应的内容。
// 异步获取
uni.getStorage({
key: 'my-Key-one',
success: res => {
console.log(res.data); //第一个Key
},
});
uni.removeStorage:从本地缓存中异步移除指定 key。
// 异步清除
uni.removeStorage({
key: 'my-Key-one',
});
uni.clearStorage():清理本地数据缓存。
uni.clearStorage();
同步
uni.setStorageSync:将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
// 同步
uni.setStorageSync('my-key-two', '第二个Key');
uni.getStorageSync:从本地缓存中同步获取指定 key 对应的内容。
// 同步获取
const keyTwo = uni.getStorageSync('my-key-two');
uni.removeStorageSync:从本地缓存中同步移除指定 key。
// 同步清除
uni.removeStorageSync('my-key-two');
uni.clearStorageSync():同步清理本地数据缓存
uni.clearStorageSync();
数据请求
uni.request:发起网络请求
import { onReady, onShow } from '@dcloudio/uni-app';
import { ref } from 'vue';
// 图片地址
const imgUrl = ref('');
// 请求图片函数
const getAvatar = () => {
// loadind
uni.showLoading({
title: '图片加载中......',
});
uni.request({
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded',
},
url: '请求地址',
data: { account: 'root' },
success: res => {
imgUrl.value = (res.data as any).data.avatar;
},
complete: () => {
uni.hideLoading();
},
});
};
onReady(() => {
getAvatar();
});