Read exported variable from remote js file

后端 未结 2 1056
傲寒
傲寒 2021-01-27 15:21

I have a javascript file that I need to read. I managed to read it as a String using FileReader, but I want to read the object that is exported in that file.

This is how

2条回答
  •  梦谈多话
    2021-01-27 15:50

    The Fetch API does not load JS modules, but files. It does not evaluate your JavaScript file.

    I would instead use a bundler for the source code (such as webpack). Then you'll be able to use your JavaScript module with require():

    // either this
    const carColors = require('./carColors');
    // or this
    import carColors from './carColors';
    
    console.log(carColors.audi);
    

提交回复
热议问题