How to get started with Angular 6.0
Learn what’s new and build an app
Angular has released its latest version, Angular 6.0. In this article, we will understand the new features of Angular 6.0 and also set up a new project with the help of Angular CLI 6.0 and Visual Studio Code.
What’s new in Angular 6.0?
ng update
A new CLI command that will update your project dependencies to their latest versions.
ng add
Another new CLI command that makes adding new capabilities to your project easier.
Angular Elements
This is a new feature that allows us to compile Angular components to native web components which we can use in our Angular app.
<template> element is deprecated
You can’t use <template> anymore inside of your component templates. You need to use <ng-template> instead.
Library support
Angular CLI now has the support for creating and building libraries. To create a library project within your CLI workspace, run the following command: ng generate library <name> (for example: ng generate library my-demo-lib)
Angular Material Starter Components
If you run “ng add @angular/material” to add material to an existing application, you will also be able to generate 3 new starter components:
- Material Sidenav
A starter component including a toolbar with the app name and the side navigation - Material Dashboard
A starter dashboard component containing a dynamic grid list of cards - Material Data Table
A starter data table component that is pre-configured with a datasource for sorting and pagination
Workspace support
Angular CLI now has support for workspaces containing multiple projects, such as multiple applications and/or libraries.
The “.angular-cli.json” file has been deprecated
Angular projects will now use “angular.json” instead of “.angular-cli.json” for build and project configuration.
Use RxJS V6
Angular 6 will also allow us to use RxJS V6 with our application.
Tree Shakable Providers
Angular 6.0 allows us to bundle services into the code base in modules where they are injected. This will help us to make our application smaller.
For example: Earlier, we used to reference our services as below.
// In app.module.ts
@NgModule({
...
providers: [MyService]
})
export class AppModule {}
// In myservice.ts
import { Injectable } from '@angular/core';
@Injectable()
export class MyService {
constructor() { }
}
This approach will still work, but Angular 6.0 provides a new and easier alternative to this. We no longer need to add references in our NgModule. We can inject the reference directly into the service. Therefore, we can use the service as below:
// In myservice.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class MyService {
constructor() { }
}
Those are the new features/improvements in the latest release of Angular. Let’s move on and create our first application using Angular 6.0.
Prerequisites
Installing Node.js will also install npm on your machine. After installing Node.js, open the command prompt and run the following set of commands to check the version of Node and npm installed on your machine.
Refer to the below image:
Now that we’ve installed Node and npm, the next step is to install Angular CLI. Run the following command in a command window. This will install Angular 6.0 CLI globally on your machine.
Open VS Code and navigate to View >> Integrated Terminal.
This will open a terminal window in VS Code.
Type the following sequence of commands in the terminal window. These commands will create a directory with the name “ng6Demo” and then create an Angular application with the name “ng6App” inside that directory.
- mkdir ng6Demo
- cd ng6Demo
- ng new ng6App
There we go — we have created our first Angular 6 application using VS Code and Angular CLI. Now run the following command to open the project.
Refer to the image below:
This will open the code file of our application in a new VS Code window. You can see the following file structure in Solution Explorer.
Notice that the folder structure is a little bit different from the older version of Angular. We have a new “angular.json” file instead of the old “.angular-cli.json” file. This config file will still serve the same task as before, but the schema has changed a bit.
Open the package.json file and you can observe that we have the latest Angular 6.0.0 packages installed in our project.
{
"name": "ng6-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.0",
"@angular/common": "^6.0.0",
"@angular/compiler": "^6.0.0",
"@angular/core": "^6.0.0",
"@angular/forms": "^6.0.0",
"@angular/http": "^6.0.0",
"@angular/platform-browser": "^6.0.0",
"@angular/platform-browser-dynamic": "^6.0.0",
"@angular/router": "^6.0.0",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.0",
"@angular-devkit/build-angular": "~0.6.0",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.0",
"@angular/language-service": "^6.0.0",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~1.4.2",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
}
The name of our Angular application is ng6app which is inside ng6demo directory.
So, we will first navigate to our application using the below commands.
And then we use the following command to start the web server.
After running this command, you can see that it is asking to open http://localhost:4200 in your browser. So, open any browser on your machine and navigate to this URL. Now, you can see the following page.
Now we will try to change the welcome text on the screen. Navigate to /src/app/app.component.ts file and replace the code with the below code.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Csharp Corner';
}
Now open the browser, you can see that the web page has been updated with new welcome message “Welcome to Csharp Corner!”
In this article we learned about the new features of Angular 6.0. We have installed the Angular 6.0 CLI and created our first Angular 6.0 application with the help of Visual Studio Code. We have also customized the welcome message on the webpage.
You can also find this article at C# Corner.
You can check my other articles on Angular here
Originally published at https://ankitsharmablogs.com/