Skip to content

Commit

Permalink
docs📝: 更新小程序文档;
Browse files Browse the repository at this point in the history
  • Loading branch information
simply-none committed Nov 3, 2022
1 parent 4cbdf07 commit dd38b19
Show file tree
Hide file tree
Showing 4 changed files with 953 additions and 242 deletions.
4 changes: 3 additions & 1 deletion _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
- [第二阶段知识点](usage-frame/typescript二期知识点.md)
- [奇怪或有用的代码](usage-frame/typescript-code.md)
- 小程序
- [微信小程序](usage-frame/微信小程序.md)
- 微信小程序
- [组件](usage-frame/wechat-mini-program/组件.md)
- [基础能力](usage-frame/wechat-mini-program/基础能力.md)
- 微前端
- [qiankun](usage-frame/qiankun.md)

Expand Down
81 changes: 81 additions & 0 deletions usage-frame/wechat-mini-program/基础能力.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 基础能力

## 存储

定义:每个小程序都有自己的本地缓存,可以通过storage进行增删改查

隔离策略:
- 同一个微信用户,同一个小程序的storage上限为10MB
- storage以用户为维度,不同用户数据、不同小程序数据无法相互调取
- 同一小程序使用不同插件,这些不同的插件之间、插件和小程序之间的storage无法相互调取
- 不同小程序使用同一插件,这个插件的storage不互通

清理策略:本地缓存的清理时机和代码包一样,只有在代码包被清理时storage才会被清理

用法:
- `wx.setStorage(obj)`:将数据存在指定的key中,会覆盖原有key的内容,若非用户主动删除或因存储原因被系统删除,该数据一直可用。单个key上限1MB(开启加密后上限0.7MB),所有key上限为10MB(开启加密后上限为7.1MB),支持promise形式的回调,obj的参数有:
- key:键名
- data:键值,支持原生类型、Date、能被JSON.stringify序列化的对象
- encrypt:是否开启加密存储,只有异步的接口支持开启,开启后,接口回调耗时增加,同时setStorage和getStorage同时需要设置该属性,且加密后数据比原始数据膨大1.4倍
- success/fail/complete:接口调用成功、失败、结束时的回调
- `wx.setStorageSync(key, data)`:同步存储,只应用于数据的持久化存储,太多会影响启动耗时
- `wx.getStorage(obj)`:obj参数比setStorage少了一个data属性,支持promise形式的回调,其中:
- success:接口返回res,使用res.data获取该key的值
- `wx.getStorageSync(key)`;同步获取缓存
- `wx.removeStorage(obj)`:移除指定key的缓存,参数比setStorage少了一个data、encrypt属性,支持promise形式的回调
- `wx.removeStorageSync(key)`:同步移除缓存,支持promise形式的回调
- `wx.clearStorage(obj)`:清理本地所有缓存,参数obj只有success、fail、complete三个函数属性,支持promise形式的回调
- `wx.clearStorageSync()`:同步清理所有缓存,无参,支持promise形式的回调

```js
// 设置缓存
wx.setStorage({
key: 'user-info',
value: data,
// 同时开启
encrypt: true,
success () {
wx.getStorage({
key: 'user-info',
// 同时开启
encrypt: true,
success (res) {
console.log(res.data)
}
})
}
})

// 同步存储
try {
wx.setStorageSync('user-info', data)
} catch (e) {
console.log(e)
}

// 获取缓存
wx.getStorage({
key: 'user-info',
encrypt: true,
success (res) {
console.log(res.data)
}
})

wx.getStorageSync('user-info')

// 清除key为user-info的缓存
wx.removeStorage({
key: 'user-info',
success () {}
})

wx.removeStorageSync('user-info')

// 清空缓存
wx.clearStorage({
success () {}
})

wx.clearStorageSync()
```
Loading

0 comments on commit dd38b19

Please sign in to comment.