博客 / 詳情

返回

uniapp項目實踐總結(十四)封裝存儲和路由方法

導語:在日常 APP 開發過程中,經常要用到數據的存儲、獲取和刪除等操作以及頁面導航之間的跳轉,為此,封裝了一個兩個簡單的方法來統一調用。

目錄

  • 原理分析
  • 方法實現
  • 實戰演練
  • 案例展示

原理分析

主要是以下 API。

  • uni.setStorage:保存數據到本地緩存中;
  • uni.getStorage:獲取保存的緩存數據;
  • uni.removeStorage:移除保存的數據緩存;
  • uni.clearStorage:清空保存的緩存數據;
  • uni.navigate{type}:跳轉頁面;

以下方法存於根目錄下的scripts文件夾下的utils.js文件中。

方法實現

接下來一一説明如何實現數據緩存操作和路由跳轉的封裝。

數據緩存

這裏是使用一個方法,通過傳入不同的類型和參數來實現。

參數如下:

  • type: 類型,包括設置,獲取,刪除,清空;
  • isSync: 是否異步;
  • key: 鍵名;
  • val: 值;
// 存儲數據
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("數據緩存參數:", 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,
    };
  }
}

路由操作

這裏是把常用的路由方法裝進一個方法裏面了,方便調用。

參數如下:

  • type: 路由類型;
  • url: 路由地址
  • key: 鍵名;
  • delta: 返回級數;
// 頁面跳轉
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("頁面跳轉參數:", 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;
}

實戰演練

模板內容

  • 緩存數據操作
<button class="eg-btn" @click="storeSet('set')">設置數據</button>
<button class="eg-btn" @click="storeSet('get')">獲取數據</button>
<button class="eg-btn" @click="storeSet('remove')">刪除數據</button>
<button class="eg-btn" @click="storeSet('clear')">清空數據</button>
<view class="eg-res"> 數據:{{ 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>

腳本方法

  • 定義數據
// 緩存數據
let data = ref("");
  • 數據操作

這裏為了方便都整合到一起調用了。

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(`數據${type}操作結果:`, 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(`數據${type}操作結果:`, res);
  }
  if (type == "remove") {
    let res = await proxy.$apis.utils.storeage({
      type: "del",
      isSync: true,
      key,
    });
    console.log(`數據${type}操作結果:`, res);
  }
  if (type == "clear") {
    let res = await proxy.$apis.utils.storeage({
      type: "clear",
      isSync: true,
    });
    console.log(`數據${type}操作結果:`, res);
  }
}
  • 路由方法

這裏為了方便都整合到一起調用了。

async function goPage(type, url, delta) {
  let data = await proxy.$apis.utils.navigate({
    type,
    url,
    delta,
  });
  console.log("頁面跳轉結果:", data);
}

案例展示

  • 數據緩存
  • 頁面路由

最後

以上就是封裝存儲和路由方法的主要內容,有不足之處,請多多指正。

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.