To dynamically import modules, you can use the import()
function, which returns a promise that resolves to the imported module. This function can be called anywhere in your code, even inside event handlers or functions called by user actions.
Here’s an example that demonstrates how to dynamically import a module based on a button click event:
document.getElementById("myButton").addEventListener("click", async () => {
try {
const myModule = await import("./path/to/myModule.js");
// Use the imported module here
myModule.myFunction();
} catch (error) {
console.error("Error importing module:", error);
}
});
In this example, when the button with the ID myButton
is clicked, the import()
function is called to load the myModule.js
file dynamically. Once the module is loaded, you can use its exported functions or variables as needed.
Note that the import()
function returns a promise, so you need to use await
to handle the asynchronous loading of the module. You can also use import().then()
syntax if you prefer working with promises directly.
It’s worth mentioning that dynamic importing is supported in modern browsers and Node.js. However, older browsers may not support it. In such cases, you can use transpilers or bundlers (such as Babel or Webpack) to ensure compatibility with a wider range of environments.
To learn more about dynamic module imports, you can refer to the following resources:
#javascript #dynamic-import