Read exported variable from remote js file

后端 未结 2 1057
傲寒
傲寒 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:45

    Change your file to return only json and parse it

    the file

    {
        audi: 'blue',
        bmw: 'black',
    }
    

    the load function

    loadFile = async () => {
        try {
            const response = await fetch(PATH_TO_FILE);
            const blob = await response.blob();
            let read = new FileReader();
            read.onload = function() {
                console.log(JSON.parse(read.result)); 
            };
            read.readAsBinaryString(blob); 
        }
        catch(e) {
            console.log(e);
        }
    }
    

提交回复
热议问题