Dynamically load a JavaScript file

后端 未结 28 3539
说谎
说谎 2020-11-22 06:56

How can you reliably and dynamically load a JavaScript file? This will can be used to implement a module or component that when \'initialized\' the component will dynamical

28条回答
  •  甜味超标
    2020-11-22 07:49

    There's a new proposed ECMA standard called dynamic import, recently incorporated into Chrome and Safari.

    const moduleSpecifier = './dir/someModule.js';
    
    import(moduleSpecifier)
       .then(someModule => someModule.foo()); // executes foo method in someModule
    

提交回复
热议问题