
By Yakov Fain | Article Rating: |
|
May 11, 2016 10:15 AM EDT | Reads: |
430 |

The current version of Angular is Release Candidate 1. This version changed the way how the framework is distributed – it comes as a set of scoped npm packages now. Any imports of the Angular classes will be done from @angular instead of angular2, for example:
import {bootstrap} from [email protected]/platform-browser-dynamic';
import {Component} from [email protected]/core';
The content of package.json, index.html, and the configuration of the SystemJS loader has to be changed accordingly. This post is an extract of our book Angular 2 Development with Typescript, and it’ll show you how to get started with a new Angular 2 RC.1 project.
You can find the source code of the initial angular-seed project at https://github.com/Farata/angular2typescript/tree/master/chapter2/angular-seed.
To start a new project managed by npm, create a new directory (e.g., angular-seed) and open it in the command window. Then run the command npm init -y, which will create the initial version of the package.json configuration file. Normally npm init asks several questions while creating the file, but the -y flag makes it accept the default values for all options. The following example shows the log of this command running in the empty angular-seed directory.
$ npm init -y
Wrote to /Users/username/angular-seed/package.json:
{
"name": "angular-seed",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Most of the generated configuration is needed either for publishing the project into the npm registry or while installing the package as a dependency for another project. We’ll use npm only for managing project dependencies and automating development and build processes.
Because we’re not going to publish it into the npm registry, you should remove all of the properties except name, description, and scripts. Also, add a “private”: true property because it’s not created by default. It will prevent the package from being accidentally published to the npm registry. The package.json file should look like this:
{
"name": "angular-seed",
"description": "An initial npm-managed project for Chapter 2",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
The scripts configuration allows you to specify commands that you can run in the command window. By default, npm init creates the test command, which can be run like this: npm test. Let’s replace it with the start command that we’ll be using for launching the live-server that’s we’ll add to the generated package.json a bit later. Here’s the configuration of the scripts property:
{
...
"scripts": {
"start": "live-server"
} }
You can run any npm command from the scripts section using the syntax npm run mycommand, e.g., npm run start. You can also use the shorthand npm start command instead of npm run start. The shorthand syntax is available only for predefined npm scripts (see the npm documentation at https://docs.npmjs.com/misc/scripts).
Now we want npm to download Angular to this project as a dependency. We want Angular with its dependencies to be downloaded to our project directory. We also want local versions of SystemJS, live-server, and the TypeScript compiler.
npm packages often consist of bundles optimized for production use that don’t include the source code of the libraries. Let’s add the dependencies and devDependencies sections to the package.json file right after the license line:
"dependencies": {
"@angular/common": "2.0.0-rc.1",
"@angular/compiler": "2.0.0-rc.1",
"@angular/core": "2.0.0-rc.1",
"@angular/http": "2.0.0-rc.1",
"@angular/platform-browser": "2.0.0-rc.1",
"@angular/platform-browser-dynamic": "2.0.0-rc.1",
"@angular/router": "2.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.1",
"@angular/upgrade": "2.0.0-rc.1",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"systemjs": "^0.19.27",
"zone.js": "^0.6.12"
},
"devDependencies": {
"live-server": "0.8.2",
"typescript": "^1.8.10"
}
Now run the command npm install on the command line from the directory where your package.json is located, and npm will start downloading the preceding packages and their dependencies into the node_modules folder. After this process is complete, you’ll see dozens of subdirectories in node_modules, including @angular, systemjs, live-server, and typescript.
angular-seed
├── index.html
├── package.json
└── app
└── app.ts
├──node_modules
├── @angular
├── systemjs
├── typescript
├── live-server
└── …
In the angular-seed folder, let’s create a slightly modified version of index.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Angular seed project</title>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/typescript/lib/typescript.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function (err) {console.error(err);});
</script>
</head>
<body>
<app>Loading...</app>
</body>
</html>
Note that the script tags now load the required dependencies from the local directory node_modules. The same applies to the SystemJS configuration file systemjs.config.js shown below:
System.config({
transpiler: 'typescript',
typescriptOptions: {emitDecoratorMetadata: true},
map: {
'app' : 'app',
'rxjs': 'node_modules/rxjs',
[email protected]/core' : [email protected]/core',
[email protected]/common' : [email protected]/common',
[email protected]/compiler' : [email protected]/compiler',
[email protected]/router' : [email protected]/router',
[email protected]/platform-browser' : [email protected]/platform-browser',
[email protected]/platform-browser-dynamic': [email protected]/platform-browser-dynamic'
},
packages: { 'app' : {main: 'main.ts', defaultExtension: 'ts'},
'rxjs' : {main: 'index.js'},
[email protected]/core' : {main: 'index.js'},
[email protected]/common' : {main: 'index.js'},
[email protected]/compiler' : {main: 'index.js'},
[email protected]/router' : {main: 'index.js'}, [email protected]/platform-browser' : {main: 'index.js'},
[email protected]/platform-browser-dynamic': {main: 'index.js'}
} });
In the section packages we mapped the name app to main.ts, so let’s create a directory called app inside angular-seed, and inside this directory create an main.ts file inside app, as follows.
----
import {bootstrap} from [email protected]/platform-browser-dynamic';
import {Component} from [email protected]/core';
@Component({
selector: 'app',
template: `<h1>Hello {{ name }}!</h1>`})
class AppComponent {
name: string;
constructor() {
this.name = 'Angular 2';
}
}
bootstrap(AppComponent);
Here we import Component and bootstrap from Angular, which is already preloaded into the local directory node_modules.
Start the application by executing the npm start command from the angular-seed directory, and it’ll open your browser and show the message “Loading…” for a split second, followed by “Hello Angular 2!”
That’s all there is to it for the Hello World type application.
The Angular team substantially modified the Router API in RC.1. The old (beta) version is still available in the package named router_deprecated. At the time of this writing the new router is not documented and we have to read the sources to see how it works. In particular, instead of @RouteConfig you’ll need to use @Route. Instead of @RouteParam use RouteSegment. The syntax of RouterLink is a little different as well. We’ve migrated the first book’s app that uses new Router, and you can see the code here: https://github.com/Farata/angular2typescript/tree/master/chapter3/auction.
If you already have some apps written with the router from Angular 2 Beta, you’ll be safer remaining with this router for some time until it’ll be better documented and all its new features will be implemented.
If you’re interested in learning Angular 2 either get our book or enroll into our upcoming online training.
Read the original blog entry...
Published May 11, 2016 Reads 430
Copyright © 2016 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Yakov Fain
Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain
![]() May. 12, 2016 06:00 AM EDT Reads: 582 |
By Elizabeth White ![]() May. 12, 2016 05:30 AM EDT Reads: 1,315 |
By Liz McMillan ![]() May. 12, 2016 05:00 AM EDT Reads: 1,702 |
By Elizabeth White ![]() May. 12, 2016 04:30 AM EDT Reads: 1,296 |
By Elizabeth White ![]() May. 12, 2016 04:30 AM EDT Reads: 1,722 |
By Elizabeth White ![]() May. 12, 2016 01:30 AM EDT Reads: 1,866 |
By Elizabeth White ![]() May. 12, 2016 01:00 AM EDT Reads: 1,764 |
By Elizabeth White ![]() May. 12, 2016 01:00 AM EDT Reads: 1,250 |
By Liz McMillan ![]() May. 11, 2016 10:00 PM EDT Reads: 1,745 |
By Elizabeth White ![]() May. 11, 2016 07:00 PM EDT Reads: 954 |
By Pat Romanski ![]() May. 11, 2016 06:30 PM EDT Reads: 1,802 |
By Elizabeth White ![]() May. 11, 2016 03:00 PM EDT Reads: 1,678 |
By Elizabeth White ![]() May. 11, 2016 03:00 PM EDT Reads: 1,052 |
By Elizabeth White ![]() May. 11, 2016 02:45 PM EDT Reads: 1,752 |
By Liz McMillan ![]() May. 11, 2016 02:00 PM EDT Reads: 1,706 |
By Liz McMillan ![]() May. 11, 2016 02:00 PM EDT Reads: 969 |
By Elizabeth White ![]() May. 11, 2016 12:45 PM EDT Reads: 1,866 |
By Elizabeth White ![]() May. 11, 2016 12:00 PM EDT Reads: 1,452 |
By Liz McMillan ![]() May. 11, 2016 11:30 AM EDT Reads: 1,823 |
By Liz McMillan ![]() May. 11, 2016 10:00 AM EDT Reads: 1,452 |