顺带这是我平时公司切换改变网络环境
直接上代码,我相信就可以懂了,
//app.js function fetchApi(url, type, params, method) { return new Promise((resolve, reject) => { wx.request({ url: `${url}/${type}`, data: params, method: method, header: { 'content-type': 'application/x-www-form-urlencoded' }, success: resolve, fail: reject }) }) } module.exports = { // 登录 GetLogin: function(params) { return fetchApi(url1, 'wx/login.html', params, 'POST') .then(res => res.data) }, } // login.js var api = require('../../utils/api.js'); //引入api.js页面 nextClick: function() { var params = { dataFrom: xxx, code: xxx, identity: xxx, username: xxx, password: xxx, } api.GetLogin(params) .then(res => { //你要执行的代码 }) }