Upgrade to Pro — share decks privately, control downloads, hide ads and more …

9. Routing and Navigation

iliya
March 06, 2017
60

9. Routing and Navigation

iliya

March 06, 2017
Tweet

Transcript

  1. Routing Process URL Router Config Router State Instantiate components and

    build component tree The router navigation is URL based
  2. base href Add <base> element to the index.html as the

    first child in the <head> to tell the router how to compose navigation URLs. <base href="/"> (app folder is the application root)
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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
  8. 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
  9. 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.
  10. Preloading Load lazy loaded modules after the main bundle is

    bootstraped and the user is interacting with the app.
  11. 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