Directory Structure
config.js
myShowLoading = const (info, Time) => { wx.showLoading ({ title: info, }); the setTimeout ( function () { wx.hideLoading (); }, Time) } const the baseUrl = 'HTTPS: // WWW. topmy.cn/api '
// variables defined exposure
module.exports = { myShowLoading: myShowLoading, the baseUrl: the baseUrl }
http.js
let config = require('config.js')//引入变量 //定义http类 class HTTP{ request(params){ wx.request({ url: config.baseUrl + params.url,//使用变量 data: params.data, header: {}, method: params.method || 'GET', dataType: 'json', responseType: 'text', success: (res)=> { let code = res.data.status; if(code == 1){ params.success(res); }else{ console.log(res.data.message); wx.showToast({ title: res.data.message, icon: 'none', duration: 2000 }) } }, fail: function (res) { }, complete: function (res) { }, }) } } //暴露定义的类 export { HTTP }
index.js
{} from the HTTP Import '../../utils/http.js' ; // class definition introduced the let HTTP = new new the HTTP (); / / instantiate class Page ({ / * * * page initial data * / Data: { caseType: [] }, // case list getCaseType: function () {
// classes defined using HTTP .request ({ URL: '/ get_category_type' , Data: { "position_id": 2 }, Method: 'the POST' , Success: (RES) => { the console.log (res.data); the this .setData ({ caseType: res.data.data }) } }) }, / * * * lifecycle function - monitor page load * / the onLoad: function (Options) { the this .getCaseType (); } })
Note: When introducing a variable or class, you need to use a relative path, or file not found error.