小程序请求数据的时候需要显示正在加载中的提示, 提升用户的体验, 所以就封装了个简单的请求, 实现这个功能.
废话不多说, 直接上代码
- var requestEvest = {
- url: '',
- data: {},
- method: '',
- header:{},
- success: function (res) {
- },
- fail: function () {
- },
- complete: function () {
- }
- }
- function request(requestEvest) {
- let url = requestEvest.url;
- let data = requestEvest.data;
- let method = requestEvest.method;
- let header = requestEvest.header;
- wx.showLoading({
- title: '加载中',
- })// 更新之后的小程序文档要求该显示框和隐藏的同时使用
- wx.request({
- url: url,
- data: data,
- method: method,
- header:header,
- success: function (res) {
- wx.hideLoading();
- requestHandler.success(res)
- },
- fail: function () {
- wx.hideLoading();
- requestHandler.fail();
- },
- complete: function () {
- }
- })
- }
- module.exports = {
- request: request
- }
在需要用到的 JS 的顶部引入, 记住要在 Page({}) 之前哟! 比如
var wxx = require('../../utils/request.js'), 注意你的路径
请求数据和之前一样, 不过是 wxx.request({}) 咯
走过路过, 留个脚印
来源: http://www.qdfuns.com/article/48855/b5b0c622ba8831fa7602388760253042.html