Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Angular Router

Slide 3

Slide 3 text

Routing Process URL Router Config Router State Instantiate components and build component tree The router navigation is URL based

Slide 4

Slide 4 text

base href Add element to the index.html as the first child in the to tell the router how to compose navigation URLs. (app folder is the application root)

Slide 5

Slide 5 text

Setting Router Config • RouterModule.forRoot(routes) - creates the router service • RouterModule.forChild(routes) - it will load the service from the root. It only registers routes.

Slide 6

Slide 6 text

Router Config Example (1)

Slide 7

Slide 7 text

Router Config Example (2)

Slide 8

Slide 8 text

Router Config Example (3)

Slide 9

Slide 9 text

First-match wins strategy More specific routes should be placed above less specific routes. 
 1. Routes with a static path. 2. Empty path route, that matches the default route. 3. The wildcard route comes last because it matches every URL.

Slide 10

Slide 10 text

Router directives • RouterLink
 • RouterLinkActive
 • RouterOutlet

Slide 11

Slide 11 text

RouterLink & RouterLinkActive example

Slide 12

Slide 12 text

Router Outlet

Slide 13

Slide 13 text

Route definition with a parameter

Slide 14

Slide 14 text

RouterModule Providers • ActivatedRoute
 • Router
 https://github.com/angular/angular/blob/master/modules/ %40angular/router/src/router_module.ts

Slide 15

Slide 15 text

Query parameters and fragments

Slide 16

Slide 16 text

router.navigate 1. URL array ( + arguments) 2. Navigation Extras: • queryParams: { 'key': value }, • fragment: 'anchor' • preserveQueryParams - Preserves the query parameters for next navigation. • preserveFragment - Preserves the fragment for next navigation • skipLocationChange - Navigates without pushing a new state into history. • replaceUrl - Navigates while replacing the current state in history.

Slide 17

Slide 17 text

router.navigate example

Slide 18

Slide 18 text

ActivatedRoute • url: An Observable of the route path(s), represented as an array of strings for each part of the route path. • data: An Observable that contains the data object provided for the route. Also contains any resolved values from the resolve guard. • params: An Observable that contains the required and optional parameters specific to the route. • queryParams: An Observable that contains the query parameters available to all routes. • fragment: An Observable of the URL fragment available to all routes. • outlet: The name of the RouterOutlet used to render the route. For an unnamed outlet, the outlet name is primary.

Slide 19

Slide 19 text

ActivatedRoute example

Slide 20

Slide 20 text

route.snapshot • The route.snapshot provides the initial value of the route parameters. • You can access the parameters directly without subscribing or adding observable operators

Slide 21

Slide 21 text

route.snapshot example

Slide 22

Slide 22 text

Configure Redirects • RedirectTo - route that we want to redirect to • pathMatch Full - match the redirect route with full URL • pathMatch Prefix - match the redirect route when the remaining URL begins

Slide 23

Slide 23 text

Redirect example

Slide 24

Slide 24 text

Child route configuration

Slide 25

Slide 25 text

Wildcard route • A wildcard route has a path consisting of two asterisks. It matches every URL. The router will select this route if it can't match a route earlier in the configuration. • A wildcard route can navigate to a custom "404 Not Found" component or redirect to an existing route.

Slide 26

Slide 26 text

Wildcard route example

Slide 27

Slide 27 text

Asynchronous routing (Lazy - Loading) main.bundle.js lazy.bundle.js http://localhost:4300/ http://localhost:4300/lazy main.bundle.js is smaller => less time to download and to bootstrap

Slide 28

Slide 28 text

Lazy Loading Config Nothing else than the config changes in our applications

Slide 29

Slide 29 text

loadChildren loadChildren tells the router to fetch the module bundle when the user navigates to the route

Slide 30

Slide 30 text

Preloading Load lazy loaded modules after the main bundle is bootstraped and the user is interacting with the app.

Slide 31

Slide 31 text

Preloading Strategies • PreloadNothing • PreloadAllModules https://github.com/angular/angular/blob/ 5f3c8441e49fd5d31654c0452360c7fa18d624ed/modules/%40angular/ router/src/router_preloader.ts

Slide 32

Slide 32 text

Custom Preloading Strategy https://github.com/angular/angular/blob/ 1f3198cb505a6bb74aadf022c7142f3f4d8cbe28/aio/content/examples/ router/ts/src/app/selective-preloading-strategy.ts

Slide 33

Slide 33 text

Things we should remember • When subscribing to an observable in a component, you almost always arrange to unsubscribe when the component is destroyed. • ActivatedRoute observables are an exceptions. They are handled by the router

Slide 34

Slide 34 text

Questions?