我有一个主要是静态的 js 文件。除了一组值之外,我根本不更改它。
是否可以将该数组移出主 js 文件并将其导入?
现在这是我的js:
sw.js:
// array that changes often
const assets = ['/','index.html','long','list','of','assets'];
// lot of code that rarely changes here
这是我想做的:
sw.js:
// array that changes often
const assets = require("assets.js");
// lot of code that rarely changes here
Assets .js:
const assets = ['/','index.html','long','list','of','assets'];
如果我必须使用 ES5 或 ES6 来完成此操作,请告诉我。
相关:
离线服务人员
https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers
最佳答案
答案是肯定的,这是可能的。确保您声明的 assets
变量不在函数
内部,以便您可以全局访问它。
例如
<script src="assets.js"><script> //declare const assets = ['/','index.html','long','list','of','assets']; here
<script src="sw.js"><script> // you can access the variable *assets* from here.
关于javascript - 是否可以将数组移出主js文件并将其导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61662490/