微信小程式api介面呼叫用封裝

2021-10-02 21:06:37 字數 1275 閱讀 7632

1. 在 02-專案 下新建request目錄及index.js檔案

1.1 02-專案\request\index.js

// 同時傳送非同步**的次數

let ajaxtimes=0;

export const request=(params)=>;

if(params.url.includes("/my/"))

ajaxtimes++;

// 顯示載入中 效果

wx.showloading();

// 定義公共的url

const baseurl="";

return new promise((resolve,reject)=>,

fail:(err)=>,

complete:()=>

}});})}

02-專案**\02-專案\pages\index

// 0 引入 用來傳送請求的 方法 一定要把路徑補全

import from "../../request/index.js";

page(,

// 頁面開始載入 就會觸發

onload: function (options) )

// }

// });

this.getswiperlist();

this.getcatelist();

this.getfloorlist();

},// 獲取輪播圖資料

getswiperlist())

.then(result => )

})},

// 獲取 分類導航資料

getcatelist())

.then(result => )

})},

// 獲取 樓層資料

getfloorlist())

.then(result => )

})},

})

微信小程式封裝API介面

2.新建資料夾http,api是封裝的介面函式,env是設定公共訪問的url,http是二次封裝wx.request 3.env.js 設定公共訪問的url,即環境變數 module.exports 測試環境 test 線上環境 引入env中的url const require env.js pro...

微信小程式開發API封裝

專案url相通部分,減輕 量,同時方便專案遷移 get請求,不需要傳參,直接url呼叫 function getdata url,dosuccess ethod get success function res 這裡時匯出 module.exports.getdata getdata page裡面隨...

微信小程式 API 介面 (2)

由於每個 api 引數 物件的屬性都有 success,fail,complete,所以在這個提前介紹,就不再每個api 上寫了 success 型別 函式 介面呼叫成功的 函式 fail 型別 函式 介面呼叫失敗的 函式 complete 型別 函式 介面呼叫結束的 函式 呼叫 成功,失敗,都會執...