简体   繁体   中英

How to import the object from React file to plain vanila javascript file

I need help to find out the way to import the object from react file to plan the vanilla javascript file. The react js file is the config file which is exporting the multiple URLs from that file, just like below:

let API;
module.exports = {
 API: 'https://localhost/3000/general/'
}

I would like to use this API path in the different plan javascript file.

When I use it using the below code,

    import { API } from "../src/config";
function sendRequest(query) {
        var data = JSON.stringify(query);
        var url = API

        // Send Request.
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                 console.log(xhr.response);
            }
        };
        xhr.open("POST", url, true);
        xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
        xhr.send(data);

    }

It is giving me an error : can not use import statement outside a module

I tried with ' require ' as well and it is also giving error : Uncaught ReferenceError: require is not defined

Any lead would be appreciated!

export default {
 API: 'https://localhost/3000/general/'
}
import { API } from "../src/config";

do like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM