CS5220 Advanced Topics in Web Programming Angular – Routing

Slides:



Advertisements
Similar presentations
LiNC Developer Meetup Welcome!. Our job is to make your life easier APIs Tools and workflow Documentation Stay in touch: developers.lithium.com Join the.
Advertisements

XP Browser and Basics1. XP Browser and Basics2 Learn about Web browser software and Web pages The Web is a collection of files that reside.
Browser and Basics Tutorial 1. Learn about Web browser software and Web pages The Web is a collection of files that reside on computers, called.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Bloglines.com How to use bloglines By: Jake Szymanski.
Adagio4 Web Content Management EP Information Offices.
Server-side Scripting Powering the webs favourite services.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
Tutorial 1 Getting Started with Adobe Dreamweaver CS3
Mohammed Mohsen Links Links are what make the World Wide Web web-like one document on the Web can link to several other documents, and those.
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
XP New Perspectives on Browser and Basics Tutorial 1 1 Browser and Basics Tutorial 1.
Publishing a Macromedia Flash Movie – Lesson 131 Publishing a Macromedia Flash Movie Lesson 13.
Tutorial 1: Browser Basics.
Multi-Part Requests/ Parent & Child Service Items.
Exploring Office 2003 – Grauer and Barber HTML And Basic Web page.
1 Chapter 12: Form Builder Objects and Flexible Code.
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)
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
Expense Auditing Process
Jim Fawcett CSE686 – Internet Programming Spring 2014
CS3220 Web and Internet Programming Introduction to Java Servlets
Data Virtualization Tutorial… SSL with CIS Web Data Sources
Services & Dependency Injection
Introduction to .NET Florin Olariu
Program Management Portal (PgMP): What’s New in R8 for the Client
WWU Hackathon May 6 & 7.
MVC Architecture. Routing
Data Virtualization Tutorial… CORS and CIS
WWU Hackathon May 6 & 7.
Making big SPA applications
MOCKUP, FLOW, AND API SERVICE
Web DevelopmEnt Angular 4
Build Better Apps with Angular 2.
Core LIMS Training: Advanced Administration
Build Better Apps with MEAN.
Getting Started with Dreamweaver
GIS - NetmapWEB Training Slides
Central Document Library Quick Reference User Guide View User Guide
Introduction to AngularJS
CS5220 Advanced Topics in Web Programming Angular – Routing
Chengyu Sun California State University, Los Angeles
FASTER Processing Florida User Group 2018.
IBM SCPM Basic Navigation
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
Chengyu Sun California State University, Los Angeles
Web API with Angular 2 Front End
Lecture 12: The Fetch Api and AJAx
Angular 2 : CRUD Operations
Chengyu Sun California State University, Los Angeles
Dongwhan Kim Annie Zhao Steven Lawrance
CS4961 Software Design Laboratory Understand Aquila Backend
Rules and Tips for Good Web Programming (and Programming in General)
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
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
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 { }

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

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

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> Allow setting link with dynamic data from code <a [routerLink]="'block1'">Block 1</a> <a [routerLink]="['block1']">Block 1</a>

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 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'] ); }

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 } } );

Specify Path Fragment <a routerLink="block2'' fragment="anchor"> this.router.navigate( ['block2'], { fragment: 'anchor' } );

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()

Example: Getting Parameters constructor( private router: Router, private route: ActivatedRoute ) {} ngOnInit() { this.paramsSub = this.route.params.subscribe( params => { this.id = params['id'] || 0; this.x = params['x']; this.y = params['y']; }); }

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, children: [ { path: 'block4', component: Block4Component }, { path: 'block5', component: Block5Component } ] } Children components would appear in the <router-outlet> in the parent

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

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

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

Add A Route Guard Use Angular CLI to generate a guard Add it to the route that needs to be protected, e.g. { path: 'block7', component: Block7Component, canActivate: [CanActivateGuard] } More examples in TechIT 2 Client

Readings Angular Router by Victor Savkin