The cache data uni-app
There before clang clang clang ~~~ wrote a blog navagator routing, today to share with you about the data cache
1.uni.setStorage (OBJECT) and uni.getStorage (OBJECT) are both asynchronous cache, it simply is to put the data in a local cache specified key, a deposit a take nothing
uni.setStorage (OBJECT): The data cache specified in the local cache key, the original will overwrite the contents of the key, this is an asynchronous interface. (
参数名 类型 是否必填 说明
key String 是 本地缓存中的指定的 key
data Object/String 是 需要存储的内容
success Function 否 接口调用成功的回调函数
fail Function 否 接口调用失败的回调函数
complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
)
uni.getStorage (OBJECT): asynchronously acquire content corresponding to the specified key from the local cache (
参数名 类型 是否必填 说明
key String 是 本地缓存中的指定的
key
success Function 是 接口调用的回调函数
res = {data: key对应的内容}
fail Function 否 接口调用失败的回调函数
complete Function 否 接口调用结束的回调函数(调用成功、失败都会执行)
)
With everyone talk about that example, recently the company to do the project, involving a store address, the address of the local cache information specified URL in the key operating data stored in the data fetch, Ado, the code is ~~
// This is the url I defined
the let url = `/ Pages and the / Newhouse / NewHouseDetail? The above mentioned id = $ {}` the above mentioned id
// the URL of specific data
console.log (url) // / Pages and the / Newhouse / NewHouseDetail? The above mentioned id = 43
The next step will be to cache data locally
I // This is the cached information will be provided to a local data cache url uni.setStorage ({ Key: 'url_key', name ------ key is url Data: url, Data stored ------ url address of my success: function () {------ callback function after a successful uni.navigateTo I want to jump page
console.log (1111) ------ successful print buffer 1111 and jump turn the page // store after a successful jump listing details page uni.navigateTo ({ url }) } })
Next read cache data locally
uni.getStorage ({
key: 'url_key',
Success: function (RES) {
the console.log ( 'which is to obtain the content key', RES)
}
}) // this is the content key acquired in the {data: " / pages / newHouse / NewHouseDetail id =
41 ", errMsg:"? getStorage: ok "}
Green labeled as I get to the data in the local cache, res.data url address what we need, and if you need direct access to it, gave you this step shows uni.setStorage (OBJECT) and uni.getStorage (OBJECT) overall process
2.uni.setStorageStnc (KEY, DATA) and uni.getStorage (KEY) In fact, the first one with essentially no difference, but is an asynchronous synchronization fills a parameter description and writing show you
uni.setStorageStnc (KEY, DATA) of the designated data stored in the local cache key, the same key will overwrite the contents of a corresponding, which is a synchronous interface
参数名 类型 是否必填 说明
key String 是 本地缓存中的指定的 key
data Any 是 需要存储的内容,只支持原生类型、及能够通过 JSON.stringify 序列化的对象
Or in the url address, for example, the above example is based on
uni.setStorageSync ( 'url_key', 'url '); my url information is stored in the url_key
uni.getStorageStnc (KEY) synchronization acquisition key corresponding to the specified content from the local cache.
参数名 类型 是否必填 说明
key String 是 本地缓存中的指定的 key
Get synchronized cache key in address information
uni.getStorageSync value = const ( 'url_key'); // assign the information I get from the key to value
3.uni.removeStorage (OBJECT) everything is the same with the foregoing ~~~ uni.removeStorageSync (KEY) which are both removed from the local cache in the specified key is behind and above is asynchronous to synchronous asynchronous Example ~ ~ ~ ~
uni.getStorage ({ Key: 'URL', Success: function (RES) { the console.log (RES) ------ print information acquired asynchronously uni.removeStorage ({ Key: 'URL', Success: function () { the console.log ( 'successfully removed') ----- after removal of the content key acquisition success in } }) } })
This is my print out data via a click event, this is the show I successfully removed, and then the ~ ~ ~ ~ so much more when you click on will find that he is not printed and you might wonder that because I have the key corresponding to the data removed Yeah, so of course, is nothing to print it ~ ~ ~
4.uni.clearStorage () and uni.clearStorageSync () which are both clean the cache local data or an asynchronous course of a synchronous followed by a fact that is the same as to say that it emmmmm cleared all your local data on a just clears the contents of the specified key in your local
Emmmm is written directly to write! ! ! uni.clearStorage () and uni.clearStorageSync () ha ha ha ha ha
If I do, badly written, insufficient write, we also want to spend, believe me, I will try to remedy! ! Er er er er er