Runoob Tutorial ArchiveEnglish

Angular 2 JavaScript environment configuration

Angular 2 JavaScript environment configuration

In this chapter, we introduce how to configure the execution environment for Angular 2.

This chapter uses JavaScript to create Angular applications, but you can also use TypeScript and Dart to create Angular applications.

The file directory structure used in this chapter is as follows:

E5D BED 5A61 431 858 45F7864E 9D1


Create configuration file

Create directory


$ mkdir angular-quickstart
$ cd angular-quickstart

Load required libraries

We recommend using npm as the package management tool. If you haven't installed npm or are unfamiliar with it, you can check our tutorial: NPM usage introduction.

Create package.json file, the code is as follows:

package.json file:

{

"

name

"

:

"

angular2-quickstart

"

,

"

version

"

:

"

1.0.0

"

,

"

scripts

"

:

{

"

start

"

:

"

npm run lite

"

,

"

lite

"

:

"

lite-server

"

}

,

"

license

"

:

"

ISC

"

,

"

dependencies

"

:

{

"

@angular/common

"

:

"

2.0.0

"

,

"

@angular/compiler

"

:

"

2.0.0

"

,

"

@angular/core

"

:

"

2.0.0

"

,

"

@angular/forms

"

:

"

2.0.0

"

,

"

@angular/http

"

:

"

2.0.0

"

,

"

@angular/platform-browser

"

:

"

2.0.0

"

,

"

@angular/platform-browser-dynamic

"

:

"

2.0.0

"

,

"

@angular/router

"

:

"

3.0.0

"

,

"

@angular/upgrade

"

:

"

2.0.0

"

,

"

core-js

"

:

"

^2.4.1

"

,

"

reflect-metadata

"

:

"

^0.1.3

"

,

"

rxjs

"

:

"

5.0.0-beta.12

"

,

"

zone.js

"

:

"

^0.6.23

"

,

"

angular2-in-memory-web-api

"

:

"

0.0.20

"

,

"

bootstrap

"

:

"

^3.3.6

"

}

,

"

devDependencies

"

:

{

"

concurrently

"

:

"

^2.0.0

"

,

"

lite-server

"

:

"

^2.2.0

"

}

}

Because the npm official website mirror is too slow to access in China, I used the Taobao npm mirror here, and the installation method is as follows:


$ npm install -g cnpm --registry=https://registry.npmmirror.com

After execution, we can use the cnpm command to install modules:


$ cnpm install

After successful execution, a node_modules directory will be generated in the angular-quickstart directory, which contains the modules needed for this instance.


Create an Angular component

A component is the foundation and core of an Angular application; a component wraps a specific functionality, and components work together to assemble a complete application.

Generally, a component is a JavaScript class used to control a view template.

Next, we create an app directory in angular-quickstart:


$ mkdir app
$ cd app

and add the component file app.component.js with the following content:

app.component.js file:

(

function

(

app

)

{

app

.

AppComponent

=

ng

.

core

.

Component

(

{

selector

:

'

my-app

'

,

template

:

'

My first Angular application

'

}

)

.

Class

(

{

constructor

:

function

(

)

{

}

}

)

;

}

)

(

window

.

app

||

(

window

.

app

=

{

}

)

)

;

Next, we will analyze the code above:

We created a visual component named AppComponent by chaining calls to the Component and Class methods in the global Angular core namespace ng.core.

The Component method accepts a configuration object containing two properties, and the Class method is where we implement the component itself; in the Class method, we add properties and methods to the component, which will be bound to the corresponding view and behavior.

Module

Angular applications are modular. ES5 does not have a built-in module system, so a third-party module system can be used. Then, we create an independent namespace app for the application, and file code can be wrapped in an IIFE (Immediately Invoked Function Expression):


(function(app) {
})(window.app || (window.app = {}));

We pass the global app namespace object into the IIFE, initializing it with an empty object if it does not exist.

Most application files output code by adding things to the app namespace. We output AppComponent in app.component.js.


app.AppComponent =

Class definition object

In this instance, the AppComponent class has only an empty constructor:


.Class({
    constructor: function() {}
});

When we want to create an application with actual meaning, we can extend this object using properties and application logic.

Component definition object

ng.core.Component() tells Angular that this class defines an Angular component. The configuration object passed to ng.core.Component() has two fields: selector and template.

ng

.

core

.

Component

(

{

selector

:

'

my-app

'

,

template

:

'

My first Angular application

'

}

)

The selector defines a simple CSS selector my-app for a host HTML element. When Angular encounters a my-app element in the host HTML, it creates and displays an AppComponent instance.

The template attribute contains the component's template.


Add NgModule

An Angular application is composed of Angular modules, which contain the components and anything else required by the Angular application.

Next, we create the app/app.module.js file with the following content:

app.module.js file:

(

function

(

app

)

{

app

.

AppModule

=

ng

.

core

.

NgModule

(

{

imports

:

[

ng

.

platformBrowser

.

BrowserModule

]

,

declarations

:

[

app

.

AppComponent

]

,

bootstrap

:

[

app

.

AppComponent

]

}

)

.

Class

(

{

constructor

:

function

(

)

{

}

}

)

;

}

)

(

window

.

app

||

(

window

.

app

=

{

}

)

)

;


Start the application

Add the app/main.js file:

app/main.js file:

(

function

(

app

)

{

document

.

addEventListener

(

'

DOMContentLoaded

'

,

function

(

)

{

ng

.

platformBrowserDynamic

.

platformBrowserDynamic

(

)

.

bootstrapModule

(

app

.

AppModule

)

;

}

)

;

}

)

(

window

.

app

||

(

window

.

app

=

{

}

)

)

;

We need two things to start the application:

Next, create index.html, the code is as follows:

index.html file:

<

html

<

head

<

meta

charset

=

"

utf-8

"

<

title

Angular 2 Instance - Runoob Tutorial (runoob.com)

</

title

<

meta

name

=

"

viewport

"

content

=

"

width=device-width, initial-scale=1

"

<

link

rel

=

"

stylesheet

"

href

=

"

styles.css

"

<

script

src

=

"

node_modules/core-js/client/shim.min.js

"

</

script

<

script

src

=

"

node_modules/zone.js/dist/zone.js

"

</

script

<

script

src

=

"

node_modules/reflect-metadata/Reflect.js

"

</

script

<

script

src

=

"

node_modules/rxjs/bundles/Rx.js

"

</

script

<

script

src

=

"

node_modules/@angular/core/bundles/core.umd.js

"

</

script

<

script

src

=

"

node_modules/@angular/common/bundles/common.umd.js

"

</

script

<

script

src

=

"

node_modules/@angular/compiler/bundles/compiler.umd.js

"

</

script

<

script

src

=

"

node_modules/@angular/platform-browser/bundles/platform-browser.umd.js

"

</

script

<

script

src

=

"

node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js

"

</

script

<

script

src

='

app

/

app

.

component

.

js

'

</

script

<

script

src

='

app

/

app

.

module

.

js

'

</

script

<

script

src

='

app

/

main

.

js

'

</

script

</

head

<

body

<

my-app

Loading...

</

my-app

</

body

</

html

index.html analysis

The execution process is: when Angular calls the bootstrapModule function in main.js, it reads the metadata of AppModule, finds AppComponent in the starting component, finds the my-app selector, locates an element named my-app, and then loads the content between this tag.

Add some styles

The code for styles.css is:

styles.css file:

h1

{

color:

#369

;

font-family:

Arial

,

Helvetica

,

sans-serif

;

font-size:

250

%

;

}

body

{

margin:

2

em

;

}

Open the terminal and enter the following command:


$ npm start

Access http://localhost:3000/, and the browser will display the result:

776FFFC AC93 4 D 9 D7 F5A6138777D6

This completes the creation of our first Angular2 application. The source code used in this article can be downloaded in the following way, excluding node_modules.

Download source code