Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading angular2 library using systemJs

I was following the Angular2 quickstart guide from angular.io They utilize systemJs for app loading.

angular2, rx.js and angular2.dev.js are loaded via script reference instead of importing ..

Is there anyway to import these scripts ?

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {        
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>
  </head>

  <!-- 3. Display the application -->
  <body>
    <app>Loading...</app>
  </body>
like image 711
runtimeZero Avatar asked Jun 29 '26 12:06

runtimeZero


1 Answers

Including the Angular2 bundles directly registers its packages using the System.register method.

That said you can also configure SystemJS to load Angular2 files by files for each modules using the following configuration but it's less efficient:

<script src="node_modules/es6-promise/dist/es6-promise.js"></script>
<script src="node_modules/es6-shim/es6-shim.js"></script>

<script src="node_modules/angular2/bundles/angular2-polyfills.min.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

<script>
  System.config({
    defaultJSExtensions: true,
    map: {
      angular2: 'node_modules/angular2/src',
      rxjs: 'node_modules/rxjs'
    },
    packages: {
      app: {
        defaultExtension: 'js',
        format: 'register'
      },
      angular2: {
        defaultExtension: 'js',
      }
    }
  });
  (...)
</script>

In both cases, it's possible to import corresponding modules (angular2/*, rxjs/*) in your application.

like image 189
Thierry Templier Avatar answered Jul 01 '26 03:07

Thierry Templier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!