Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use angular 2 without any module loader?

The Quickstart section on the angular 2 official site describes how to run the simpliest angular 2 application. By default SystemJS is used as module loader. Also there is a note, saying that there are alternatives that work just fine. I understand the benefits of module loaders, but is there a way to run angular 2 app without any module loader at all?

When I simply delete SystemJS configuration and SystemJS source script tag, I get an error in console in angular2.dev.js file:

Uncaught ReferenceError: System is not defined

It looks like angular 2 itself uses SystemJS. So, is there a way to use angular 2 without any module loader?

like image 233
Rybakov Avatar asked Apr 30 '16 15:04

Rybakov


1 Answers

Yes, you can use plain ES5 javascript. Try this simple example.

Also, you can try the oficial javascript 5 min quickstart where they create an example considering that "ES5 JavaScript doesn't have a native module system. There are several popular 3rd party module systems we could use. Instead, for simplicity and to avoid picking favorites, we'll create a single global namespace for our application.".

That single global namespace (and loading the correct scripts described in the tutorial (angular2-all.umd.js, etc.)) is one of the ways to run without a module loader.

This structure also works with Typescript or ES6. You only need to add your favorite transpiler (Babel, Traceur, etc.) to compile your code to ES5 and use it with the rest of the code from the tutorial, but I suppose you already have that running.

like image 190
Mono Avatar answered Oct 06 '22 23:10

Mono