如何在我的应用程序中创建JS文件?

我有一个愚蠢的库,只能从另一个JS文件加载所需的元数据。该文件看起来像这样

// customStuff.js
__customStuff = [
 {.. stuff 1},{.. stuff 2},{.. etc}
]

那个愚蠢的库使用名为custom_stuff_filename的参数,该参数是描述自定义填充文件的字符串

{
  custom_stuff_file_name: "customStuff.js"
}

customStuff.js位于看起来像myapp/public/silly_library/static/customStuff.js的公用文件夹中,而其余的源代码位于myapp/src/

如您所见,customStuff.js是静态的。仅当我放进文件中时,该文件中的东西才会坐在那里。

我不想这样。相反,我想在src中的某个文件中进行API调用,以使__customStuff是该API调用的结果。

我该怎么做? javascript中有什么方法可以保存数据到另一个javascript文件中?

a8608 回答:如何在我的应用程序中创建JS文件?

以这种方式尝试:-

componentDidMount{
    fetch("URL")
        .then(response => {
            response.blob().then(blob => {
                let url = window.URL.createObjectURL(blob);
                let a = document.createElement('a');
                a.href = url;
                a.download = '__customStuff.json';
                a.click();
            });
}
本文链接:https://www.f2er.com/3081898.html

大家都在问