api文件:
wxRequet.js文件:
const wxRequest = async (params = {}, url) => {
uni.showLoading({
title: '加载中...',
mask: true,
icon: 'none'
});
let data = params.query || {};
for (var key in data) {
if (data[key] == 'undefined' || data[key] == undefined || data[key] == 'null') {
data[key] = null
}
}
let header = params.header || {
'Content-Type': 'application/json'
};
let token = uni.getStorageSync('token')
header['X-Access-Token'] = token
let res = await uni.request({
url: params.url || url,
method: params.method || 'GET',
data: data,
header: header,
});
uni.hideLoading();
if (res != null && res.data != null && res.data.code == 200) {
console.log("one")
return res
} else if (res != null && res.data != null && res.data.code == 100) {
console.log("two")
return res
} else {
return res;
}
};
module.exports = {
wxRequest
}
导出和引入的方式对不上,一个人是
CommonJs
和 一个是ES Module
如果你想要使用
import {xxx} from xxx
来引入的话,需要使用export xxx
来导出方法。如果你想要使用
module.exports
来导出的话,需要使用const xxx = require(xxx)
来引入。另外
export
的方式不一样,对应的引入也会不同。比如说使用
export xxx
导出的方法,需要使用import {xxx} from xxx
来引入。使用
export default xxx
导出的方法,则使用import xxx from xxx
来引入。相关阅读
JavaScript 模块 - JavaScript | MDN
Module 的语法 - ECMAScript 6入门