封装好的wxRequest一直提示没导出?

image.png

image.png

api文件:
image.png
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
}
阅读 1.6k
2 个回答

导出和引入的方式对不上,一个人是 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入门

把wxRequest.js的最后改成

export  {
    wxRequest
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题