uniapp封裝存儲和路由方法詳解
原理分析
主要是以下 API。
uni.setStorage
:保存數(shù)據(jù)到本地緩存中;uni.getStorage
:獲取保存的緩存數(shù)據(jù);uni.removeStorage
:移除保存的數(shù)據(jù)緩存;uni.clearStorage
:清空保存的緩存數(shù)據(jù);uni.navigate{type}
:跳轉(zhuǎn)頁面;
以下方法存于根目錄下的scripts
文件夾下的utils.js
文件中。
方法實現(xiàn)
接下來一一說明如何實現(xiàn)數(shù)據(jù)緩存操作和路由跳轉(zhuǎn)的封裝。
數(shù)據(jù)緩存
這里是使用一個方法,通過傳入不同的類型和參數(shù)來實現(xiàn)。
參數(shù)如下:
type
: 類型,包括設(shè)置,獲取,刪除,清空;isSync
: 是否異步;key
: 鍵名;val
: 值;
// 存儲數(shù)據(jù) async function storeage(options) { try { let defultOptions = { type: options.type, isSync: options.isSync || false, key: options.key, data: options.val, }; let params = { ...options, ...defultOptions }; console.log("數(shù)據(jù)緩存參數(shù):", params); let { type, isSync, key, data } = params; let result = null, types = { set: uni[`setStorage${isSync ? "Sync" : ""}`], get: uni[`getStorage${isSync ? "Sync" : ""}`], del: uni[`removeStorage${isSync ? "Sync" : ""}`], clear: uni[`clearStorage${isSync ? "Sync" : ""}`], }; if (type == "set") { if (isSync) { result = await types[type](key, data); } else { result = await types[type]({ key, data, }); } } if (["get", "del"].includes(type)) { let param = isSync ? key : { key }; result = await types[type](param); } if (type == "clear") { result = await types[type](); } return { code: 1, data: result, }; } catch (e) { return { code: 2, data: e, }; } }
路由操作
這里是把常用的路由方法裝進一個方法里面了,方便調(diào)用。
參數(shù)如下:
type
: 路由類型;url
: 路由地址key
: 鍵名;delta
: 返回級數(shù);
// 頁面跳轉(zhuǎn) async function navigate(options) { let res = null, defultOptions = { type: options.type || "to", url: options.url || "", delta: options.delta || 1, }, params = { ...options, ...defultOptions }; if (!params.type) return; if (params.type != "back" && !params.url) return; let { type, url, delta } = params; console.log("頁面跳轉(zhuǎn)參數(shù):", params); if (type == "to") { res = await uni.navigateTo({ url, }); } if (type == "back") { res = await uni.navigateBack({ delta, }); } if (type == "redir") { res = await uni.redirectTo({ url, }); } if (type == "tab") { res = await uni.switchTab({ url, }); } if (type == "lanuch") { res = await uni.reLaunch({ url, }); } return res; }
實戰(zhàn)演練
模板內(nèi)容
- 緩存數(shù)據(jù)操作
<button class="eg-btn" @click="storeSet('set')">設(shè)置數(shù)據(jù)</button> <button class="eg-btn" @click="storeSet('get')">獲取數(shù)據(jù)</button> <button class="eg-btn" @click="storeSet('remove')">刪除數(shù)據(jù)</button> <button class="eg-btn" @click="storeSet('clear')">清空數(shù)據(jù)</button> <view class="eg-res"> 數(shù)據(jù):{{ data }} </view>
- 路由操作
<button class="eg-btn" @click="goPage('to', '/pages/test/stand')">去模板</button> <button class="eg-btn" @click="goPage('back', '', 1)">返回上一頁</button> <button class="eg-btn" @click="goPage('redir', '/pages/index/swiper')">去重定向</button> <button class="eg-btn" @click="goPage('tab', '/pages/user/index')">去我的</button> <button class="eg-btn" @click="goPage('lanuch', '/pages/index/list')">去列表</button>
腳本方法
- 定義數(shù)據(jù)
// 緩存數(shù)據(jù) let data = ref("");
- 數(shù)據(jù)操作
這里為了方便都整合到一起調(diào)用了。
async function storeSet(type) { let id = proxy.$apis.utils.uuid(), key = "1693991490699-10vrs3hoiv6"; if (type == "set") { let res = await proxy.$apis.utils.storeage({ type: "set", isSync: true, key: id, val: `id-${id}`, }); console.log(`數(shù)據(jù)${type}操作結(jié)果:`, res); } if (type == "get") { let res = await proxy.$apis.utils.storeage({ type: "get", isSync: true, key, }); if (res.code == 1) { data.value = res.data; } console.log(`數(shù)據(jù)${type}操作結(jié)果:`, res); } if (type == "remove") { let res = await proxy.$apis.utils.storeage({ type: "del", isSync: true, key, }); console.log(`數(shù)據(jù)${type}操作結(jié)果:`, res); } if (type == "clear") { let res = await proxy.$apis.utils.storeage({ type: "clear", isSync: true, }); console.log(`數(shù)據(jù)${type}操作結(jié)果:`, res); } }
- 路由方法
這里為了方便都整合到一起調(diào)用了。
async function goPage(type, url, delta) { let data = await proxy.$apis.utils.navigate({ type, url, delta, }); console.log("頁面跳轉(zhuǎn)結(jié)果:", data); }
案例展示
數(shù)據(jù)緩存
頁面路由
以上就是uniapp封裝存儲和路由方法詳解的詳細內(nèi)容,更多關(guān)于uniapp封裝存儲路由的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Bootstrap基本組件學(xué)習(xí)筆記之導(dǎo)航(10)
這篇文章主要為大家詳細介紹了Bootstrap基本組件學(xué)習(xí)筆記之導(dǎo)航,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12隨機顯示經(jīng)典句子或詩歌的javascript腳本
這篇文章主要介紹了隨機顯示經(jīng)典句子或詩歌的javascript腳本的相關(guān)資料,需要的朋友可以參考下2007-08-08瀏覽器視頻幀操作方法?requestVideoFrameCallback()
這篇文章主要介紹了瀏覽器視頻幀操作方法?requestVideoFrameCallback(),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07微信頁面彈出鍵盤后iframe內(nèi)容變空白的解決方案
當(dāng)鍵盤彈出后,頁腳也被頂起來;而當(dāng)搜索完(要刷新整體頁面),鍵盤縮回后,iframe里 鍵盤當(dāng)住的地方變成白色。怎么解決這個問題呢?下面腳本之家小編給大家分享微信頁面彈出鍵盤后iframe內(nèi)容變空白的解決方案,一起看看吧2017-09-09JavaScript中常見內(nèi)置函數(shù)用法示例
這篇文章主要介紹了JavaScript中常見內(nèi)置函數(shù)用法,結(jié)合實例形式分析了JavaScript常用內(nèi)置函數(shù)的功能、參數(shù)、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2018-05-05