Fireship · @fireship

Angular Router - The Basics and Beyond

Uploaded June 12, 2018 · Length 11:47

Angular Router - The Basics and Beyond

Summary

Learn Angular Router basics and advanced techniques for lazy loading, resolvers, and guards, with examples of dynamic routing and Firebase integration.

  • angular router
  • lazy loading
  • resolvers
  • guards
  • dynamic routing
  • firebase integration

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    the router is one of the most powerful

  3. 0:08

    yet underutilized tools and angular in

  4. 0:11

    addition to defining the URL structure

  5. 0:13

    for your app it can also be used to

  6. 0:15

    control performance with lazy loading

  7. 0:16

    and help you write dry code with

  8. 0:18

    resolvers and guards over the next few

  9. 0:21

    minutes you'll learn the basics of the

  10. 0:22

    angular 6 router plus a few advanced

  11. 0:24

    techniques to preload data from firebase

  12. 0:26

    and protect routes from unauthenticated

  13. 0:28

    users if you're new here like and

  14. 0:31

    subscribe and grab the full write-up

  15. 0:32

    from angular firebase com the first

  16. 0:35

    thing we'll do to get started is

  17. 0:36

    generate a brand new angular 6 app with

  18. 0:38

    the router flag that'll just give us a

  19. 0:40

    routing module automatically the first

  20. 0:43

    thing we'll want to look at is the

  21. 0:44

    router outlet when you route to a

  22. 0:46

    component it needs to be the child of

  23. 0:48

    some other component which by default

  24. 0:50

    will be the app component and the root

  25. 0:52

    of the project so the outlet tells

  26. 0:54

    angular where to render a component

  27. 0:55

    based on the URL that the user navigates

  28. 0:57

    to we can define our routes by going

  29. 1:00

    into the app routing module and you'll

  30. 1:02

    see we have an array that's typed to the

  31. 1:04

    routes interface we can define each

  32. 1:06

    route in our application as an object

  33. 1:08

    which will take a couple of

  34. 1:09

    configuration parameters the most basic

  35. 1:12

    use case would be to give your route up

  36. 1:14

    path and then route it to a specific

  37. 1:16

    component that you've generated let's

  38. 1:18

    run ng generate component and we'll call

  39. 1:20

    this the home page now the home page we

  40. 1:23

    could set up as the root path so we'll

  41. 1:25

    give that a path property of an empty

  42. 1:27

    string then set the component option to

  43. 1:30

    the home component if we serve the app

  44. 1:32

    we should see the home component

  45. 1:33

    rendered at the position of that router

  46. 1:35

    outlet then notice how other content in

  47. 1:38

    the home component is still there such

  48. 1:39

    as the logo and header that'd be a good

  49. 1:41

    place to put a nav bar at a footer and

  50. 1:43

    things that'll be present on every page

  51. 1:45

    that's a good start but let's look at

  52. 1:47

    how we can navigate between multiple

  53. 1:48

    components by generating another one

  54. 1:50

    called animals then we can import the

  55. 1:53

    component into our router module and set

  56. 1:55

    up another path and component property

  57. 1:57

    for this route when setting up

  58. 1:59

    hyperlinks in angular you don't use a