How to load external scripts dynamically in Angular?

前端 未结 15 1101
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:48

I have this module which componentize the external library together with additional logic without adding the

15条回答
  •  天命终不由人
    2020-11-22 07:06

    If you're using system.js, you can use System.import() at runtime:

    export class MyAppComponent {
      constructor(){
        System.import('path/to/your/module').then(refToLoadedModule => {
          refToLoadedModule.someFunction();
        }
      );
    }
    

    If you're using webpack, you can take full advantage of its robust code splitting support with require.ensure :

    export class MyAppComponent {
      constructor() {
         require.ensure(['path/to/your/module'], require => {
            let yourModule = require('path/to/your/module');
            yourModule.someFunction();
         }); 
      }
    }
    

提交回复
热议问题