Chapter I. Angular Introduction

Install

npm install -g @angular/cli

Start a new Project

This command will create a new project inside of the current path that you are

ng new testproject

Initial Structure

Tree Structure

.
β”œβ”€β”€ angular.json
β”œβ”€β”€ package.json
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ public
β”‚Β Β  β”œβ”€β”€ assets
β”‚Β Β  β”‚Β Β  └── img
β”‚Β Β  └── favicon.ico
β”œβ”€β”€ README.md
β”œβ”€β”€ src
β”‚Β Β  β”œβ”€β”€ app
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.config.server.ts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.config.ts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.css
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.html
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.routes.server.ts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.routes.ts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.spec.ts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ app.ts
β”‚Β Β  β”œβ”€β”€ index.html
β”‚Β Β  β”œβ”€β”€ main.server.ts
β”‚Β Β  β”œβ”€β”€ main.ts
β”‚Β Β  β”œβ”€β”€ server.ts
β”‚Β Β  └── styles.css
β”œβ”€β”€ tsconfig.app.json
β”œβ”€β”€ tsconfig.json
└── tsconfig.spec.json

Generally, Angular always is going to create something like this. This is the regular structure.

In app/ directory we will find the most of the files that we are going to use. in the root of this directory, app files will be generated. app.html will be the html template, we can delete all in that file, because it is just a template, if you desire, you can run it with the command in the section below.

Run an angular server

ng serve

Understanding Components structure

Each component in Angular is generated with other 3 files, these other are files that have an specific role. Let's see in what each one consists.

Info

Component is just the name of our component in this example

Now it's just important we focus in the first three files generated by a Component.

Routes in Angular

All routes of our application are stored in app.routes.ts

import { Routes } from '@angular/router';

export const routes: Routes = [];

Inside of routes we can define all routes that we are going to use in our application. For example:

import { Routes } from '@angular/router';
import { Component } from './Component/component';
import { OtherComponent } from '...';

export const routes: Routes = [
	{path : 'user/login', component : Component},
	{path : 'user/register', component : OtherComponent}
];

If we go to user/login once our server is running, we will see rendered our component, this thanks to HTML and CSS files generated with the main ts Component.

Some need commands

# To create a new Component
ng generate component ComponentName
ng g c ComponentName
References