问题
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?
回答1:
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.
回答2:
According to this blogpost you can use the TypeScript compiler to traverse the imports and bundle them. The feature in question is Concatenate AMD and System modules with --outFile. I have not tested this. Technically SystemJS is still a module loader but it is supposed to be standardized.
来源:https://stackoverflow.com/questions/36956609/is-there-a-way-to-use-angular-2-without-any-module-loader