【uniapp 第二章】

数据缓存(存储在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();
});

具体配置看文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值