我正在尝试创建一个名为helpers.js的自定义类,如下所示:

class Httprequest{
    constructor(type, url, username, password,email){
        this.type = type;
        this.url = url;
        this.request = new XMLHttpRequest();
        this.username = username;
        this.password = password;
        this.email = email;
    }
    static sendPostRequest(){

        // function(method,url,async,user,password);
        this.request.open(this.type,this.url);
        this.request.setRequestHeader('Content-Type' ,'application/x-www-form-urlencoded');
        const data = encodeURI('username=' + this.username + '&'+
                                'password=' + this.password + '&'+
                                'email=' + this.email);
        this.request.send(data);
        if(this.request.readyState === 4){
            const status = this.request.status;
                if(status === 201){
                    return this.request.status;
                }else{
                    return "Failed creating account"
                }
        }
    }

}

export default Httprequest;


然后我试图在我的React组件中导入名为Projects.js的类

import Httprequest from 'helpers.js';


我收到如下错误:
编译失败。

./src/components/Projects.js
Module not found: Can't resolve 'helpers.js' in 'C:\Users\Account\Desktop\front\frontend\src\components'


这两个文件都在同一个文件夹中,怎么办?

最佳答案

你可以尝试:

import Httprequest from './helpers.js';


您需要像这样显式指示您的当前文件夹。

10-04 21:27