+ 1
Dynamically load a js inside js
Hello, Can someone show an example of how to load js files dynamically one after another in another js file and push them in an array? I am a complete beginner with all of this and I do not know how to do it. Thank you in advance.
2 Antworten
0
You could use async function to dynamically load a js module.
Eg
// In test.js
export const fruitArray = ["apple", "orange", "durian"];
export default function() {
console.log("Dynamic module loaded");
}
// Button event triggers test.js module loading
async function load() {
let dynMod = await import('test.js');
dynMod.default();
alert(dynMod.fruitArray.join(","); // load array data
}
Check out an demo here
https://code.sololearn.com/WoR3yeFp6pSe/?ref=app