CS5220 Advanced Topics in Web Programming Angular – Routing

Slides:



Advertisements
Similar presentations
PubMed Search Options (Basic Course: Module 6). Table of Contents  History  Advanced Search  Accessing full text articles from HINARI/PubMed  Failure.
Advertisements

Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
Multi-Part Requests/ Parent & Child Service Items.
Exploring Office 2003 – Grauer and Barber HTML And Basic Web page.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Krishna Mohan Koyya Glarimy Technology Services
 ASP.NET provides an event based programming model that simplifies web programming  All GUI applications are incomplete without enabling actions  These.
CITA 310 Section 4 Apache Configuration (Selected Topics from Textbook Chapter 6)
ANGULAR 2. JavaScript is a high-level, dynamic, untyped, and interpreted programming language. JavaScript was originally developed in May 1995 by Brendan.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page.
Angularjs 2.0 : Getting started
Jim Fawcett CSE686 – Internet Programming Spring 2014
CS3220 Web and Internet Programming Introduction to Java Servlets
Services & Dependency Injection
Introduction to .NET Florin Olariu
Application Architecture, Redux
myROR.org Discussion Forum Tutorial
WWU Hackathon May 6 & 7.
First EURAXESS TOPIII training for Portal Administrators
CARA 3.10 Major New Features
Section 13 - Integrating with Third Party Tools
Techniques and Practices for Testing Angular
Data Virtualization Tutorial… CORS and CIS
WWU Hackathon May 6 & 7.
Making big SPA applications
Web DevelopmEnt Angular 4
Build Better Apps with Angular 2.
Core LIMS Training: Advanced Administration
Build Better Apps with MEAN.
GIS - NetmapWEB Training Slides
Angularjs Interview Questions and Answers By Hope Tutors.
Unit 27 - Web Server Scripting
Predefined Dialog Boxes
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
CSE 154 Lecture 22: AJAX.
Introduction to AngularJS
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Spring – Web MVC
IBM SCPM Basic Navigation
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS5220 Advanced Topics in Web Programming Angular – Routing
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming More Node.js
CS5220 Advanced Topics in Web Programming Angular – Services
Chengyu Sun California State University, Los Angeles
Lecture 12: The Fetch Api and AJAx
Web API with Angular 2 Front End
Lecture 12: The Fetch Api and AJAx
Angular 2 : CRUD Operations
Dongwhan Kim Annie Zhao Steven Lawrance
Rules and Tips for Good Web Programming (and Programming in General)
Chengyu Sun California State University, Los Angeles
ASP.NET MVC Web Development
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – TypeScript
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – Services
CS5220 Advanced Topics in Web Programming More Node.js
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Web Forms.
Presentation transcript:

CS5220 Advanced Topics in Web Programming Angular – Routing Chengyu Sun California State University, Los Angeles

The Need for Routing Usually we have different pages in a web application How do we have different "pages" in a SPA? Home About Login

Angular Routing Load different components based on different URL About Login Component Home About Login HomeComponent AppComponent

Add A Routing Module The routing option of ng new Add an AppRoutingModule Import the routing module in AppModule Add a <router-outlet> in App component template, which is a placeholder for the components to be loaded.

AppRoutingModule import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = []; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }

Routes Routes specify the mappings between paths and components, e.g. const routes: Routes = [ { path: 'about', component: AboutComponent }, { path: 'login', component: LoginComponent } ];

RouterModule RouterModule.forRoot(routes) returns a RouterModule that is configured with routes AppRoutingModule imports this module, then exports it so the rest of the application can use it You can specify routing directly in AppModule, but using an AppRoutingModule is recommended (for centralizing route management in large applications)

The forRoot() Convention forRoot() returns a module that should be imported by the root module (i.e. the module used for bootstrapping the application) Use forChild() if you want to add routing to a feature module

Basic Routing const routes: Routes = [ { path: 'block1', component: Block1Component }, { path: 'block2', component: Block2Component } ]; In template: <a routerLink="block1">Block 1</a> <a routerLink="block2">Block 2</a>

Why routerLink instead of href? See the difference using Chrome's Developer Tools (Network tab) Clicking on a routerLink does NOT send a request – the address URL is changed through HTML5 history API

routeLink as @Input <a routerLink="block1">Block 1</a> Link is hard-coded in template <a routerLink="block1">Block 1</a> <a [routerLink]="'block1'">Block 1</a> <a [routerLink]="['block1']">Block 1</a> Allow setting link with dynamic data from code

Route Segments <a routerLink="user/bob">Bob</a> <a [routerLink]="['user', name]">{{name}}</a>

Wildcard Route const routes: Routes = [ { path: 'about', component: AboutComponent }, { path: 'login', component: LoginComponent }, { path: '**', component: PageNotFoundComp} ]; Catch un-matched routes and display a PageNotFound page (i.e. component) Route order matters so make sure the wildcard route is listed last

Navigate to Route in Code Example: click a button in Block1 to go to Block2 class Block1Component { constructor(private router: Router) {} goToBlock2(): boolean { this.router.navigate( ['block2'] ); return false; }

Dependency Injection Things to inject: services and data Declare in providers of NgModule Inject in constructors

Dependency Injection: Providers Type Token providers: [ { provide: UserService, useClass: UserService }, { provide: 'SECRET', useValue: 'abcd' } ] String Token providers: [ UserService, { provide: 'SECRET', useValue: 'abcd' } ]

Dependency Injection: Inject Class HomeComponent { constructor( private UserService: userService, @Inject('SECRET') private secret: string ) {} }

Attach Information To Route Path parameters, e.g. /user/:id Matrix parameters, e.g. /user;fn=John;ln=Doe Query parameters, e.g. /user?id= Path fragment, e.g. /user#profile

Specify Route Parameters const routes: Routes = [ { path: 'block1/:id', component: Block1Component } ]; In template: <a routerLink="block1/1">Block 1</a> <a [routerLink]="['block1', 1]">Block 1</a> In component class: this.router.navigate(['block1', 1]);

Specify Matrix Parameters <a [routerLink]="['block1',{x:10, y:20}]''> this.router.navigate([ 'block1', {x:10, y:20} ]);

Specify Query Parameters <a routerLink="block2'' [queryParams]="{id: 1}"> this.router.navigate( ['block2'], { queryParams: { id: 1 } } );

Retrieve Information from Route Inject an ActivatedRoute in component constructor Subscribe to params (for both route and matrix parameters) or queryParams or fragment in ngOnInit() Unsubscribe in ngOnDestroy()

Observable and Subscription ActivatedRoute.params is a Observable An Observable represents a set of values that change over time A Subscription allows a subscriber to be notified whenever the values of an observable change

Asynchronous Programming Paradigms Callback Function for event handling Observable for continuously changing data / data stream Promise for something that eventually produces a single result

Why "Subscribing" to Parameters? Add console.log() in ngOnInit() and ngOnDestroy() to see when a component is initialized/destroyed Angular reuses components to improve performance, so the same component instance needs to handle changing parameters

Redirect pathMatch can be either 'prefix' (default) or 'full' const routes: Routes = [ { path: 'block1', component: Block1Component }, { path: 'blockx', redirectTo: 'block1', pathMatch: 'full'} ]; pathMatch can be either 'prefix' (default) or 'full'

Children Routes { path: 'block3', component: Block3Component, ] }

Named Router Outlet Template: <router-outlet><router-outlet> <router-outlet name="other"></router-outlet> Routes: { path: 'block6', component: Block6Component, outlet: 'other' }

Load Component to a Named Outlet <a [routerLink]="[{outlets: {other: ['block6']}}]"> Block 6 </a> Instead of from path to routes to component to outlet, it's more like from outlet to routes to path to component

Route Guard Determine whether a user can navigate to or away from a route Types of route guard CanActivate: navigate to a route CanActivateChild: navigate to a child route CanDeactivate: navigate away from a route CanLoad: used for lazy-loaded modules

Add A Route Guard Use Angular CLI to generate a guard Naming conventions Add it to providers in @NgModule Add it to the route that needs to be protected, e.g. { path: 'block7', component: Block7Component, canActivate: [CanActivateGuard] }

CanDeactivate CanDeactivate guard is usually used to remind the user of something before the user navigates away from a component As such, it requires a component that displays a confirmation dialog

Resolve Guard Resolve guard is used for retrieving some data before a route is activated

Readings Angular Router by Victor Savkin