AngularJS2 TypeScript environment configuration
This section is used to create Angular TypeScript application, which is the official recommended to use, examples of this tutorial will also use TypeScript to write.
TypeScript developed by Microsoft is a free and open source programming language, which is a superset of JavaScript, expanding the JavaScript syntax.
If you do not know TypeScript, you can consult the following information:
- TypeScript Getting Started Tutorial
- TypeScript Chinese manual
Before this start, you need to make sure you have installed npm, if you do not understand or did not install npm npm can see our tutorial: NPM use presentation .
Since npm country visit the official website of the mirror is too slow, and here I use Taobao npm image, installation is as follows:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
After we do that we can use to install the module cnpm command:
$ cnpm install
Step 1: Create and configure projects
Create a directory
$ mkdir angular-quickstart $ cd angular-quickstart
Create profile
Angular project requires the following profiles:
- package.json mark the desired npm this project dependencies.
- tsconfig.json defined TypeScript compiler how to generate JavaScript code from the project source files.
- typings.json provides additional definition files for those TypeScript compiler does not recognize the library.
- systemjs.config.js provides that where to find the application module information module loader, and register all the necessary dependencies.It also includes examples of documents need to use the back of the package. a
Create the following file in the angular-quickstart, the code is as follows:
package.json file:
tsconfig.json file:
typings.json file:
systemjs.config.js file:
Next we use cnpm command to install the dependencies:
$ cnpm install
After the successful implementation of the next angular-quickstart directory node_modules will generate a directory containing modules here we need this example, we can look at the directory structure of the project:
Step 2: Create the application
We NgModules the Angular applications organized into a number of functions related to code blocks.
Angular itself is split into a number of separate modules Angular, and we only need to import in the application section Angular needed.
Angular Each application requires at least aroot module (root module), for instance AppModule.
Next we create the app directory under angular-quickstart directory:
$ mkdir app $ cd app
Then create app.module.ts file in the app directory, the code is as follows:
app.module.ts file:
Since QuickStart is a Web application running in the browser, so the root module needs to import BrowserModule from @ angular / platform-browser and imports added to the array.
Create component and add it to the application
Angular Each application has at least oneroot component, for instance AppComponent, app.component.ts file code is as follows:
app.component.ts file:
Code analysis:
The above code fromangular2 / coreintroduces aComponentpack.
@ComponentIs Angular 2decorator,it will copy the metadata associated with the AppComponent component class.
my-appis a CSS selector, available in HTML tags, use as a component.
@viewcontains atemplate,how to tell Angular rendered view of the assembly.
exportspecified components can then use the documents.
Next we reopen app.module.ts file import new AppComponent, and add it to NgModule decorator declarations and bootstrap fields:
app.module.ts file:
Part IV: Start the application
Next we need to tell Angular how to start the application.
Create main.ts file in the angular-quickstart / app directory, the code is as follows:
main.ts file:
The above code initializes the platform, so you can run the code, and then start your AppModule on the internet.
Customize the application host page
Create index.html file in the angular-quickstart directory, the code is as follows:
index.html file:
Here noteworthy are:
JavaScriptlibraries: core-js is provided for older browsers fill libraries,zone.js andreflect-metadatarepository is Angular need, and SystemJS library is used to make the module loaded.
SystemJS configuration files and scripts, can be imported and run in the main file we just write the app module.
<My-app> tag is the application loading place
Add some style
We can set the style we need in styles.css file angular-quickstart directory:
styles.css file:
Step Six: Compile and run the application
Open a terminal window, enter the following command:
npm start
Visit http: // localhost: 3000 /, the browser displays the results as follows:
So our first Angular2 application created even if the final directory structure:
As used herein, the source code can be downloaded in the following ways, and does not contain node_modules typings directory.