Fireship · @fireship
Angular Router - The Basics and Beyond
Uploaded June 12, 2018 · Length 11:47
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.
- 0:00
[Music]
- 0:06
the router is one of the most powerful
- 0:08
yet underutilized tools and angular in
- 0:11
addition to defining the URL structure
- 0:13
for your app it can also be used to
- 0:15
control performance with lazy loading
- 0:16
and help you write dry code with
- 0:18
resolvers and guards over the next few
- 0:21
minutes you'll learn the basics of the
- 0:22
angular 6 router plus a few advanced
- 0:24
techniques to preload data from firebase
- 0:26
and protect routes from unauthenticated
- 0:28
users if you're new here like and
- 0:31
subscribe and grab the full write-up
- 0:32
from angular firebase com the first
- 0:35
thing we'll do to get started is
- 0:36
generate a brand new angular 6 app with
- 0:38
the router flag that'll just give us a
- 0:40
routing module automatically the first
- 0:43
thing we'll want to look at is the
- 0:44
router outlet when you route to a
- 0:46
component it needs to be the child of
- 0:48
some other component which by default
- 0:50
will be the app component and the root
- 0:52
of the project so the outlet tells
- 0:54
angular where to render a component
- 0:55
based on the URL that the user navigates
- 0:57
to we can define our routes by going
- 1:00
into the app routing module and you'll
- 1:02
see we have an array that's typed to the
- 1:04
routes interface we can define each
- 1:06
route in our application as an object
- 1:08
which will take a couple of
- 1:09
configuration parameters the most basic
- 1:12
use case would be to give your route up
- 1:14
path and then route it to a specific
- 1:16
component that you've generated let's
- 1:18
run ng generate component and we'll call
- 1:20
this the home page now the home page we
- 1:23
could set up as the root path so we'll
- 1:25
give that a path property of an empty
- 1:27
string then set the component option to
- 1:30
the home component if we serve the app
- 1:32
we should see the home component
- 1:33
rendered at the position of that router
- 1:35
outlet then notice how other content in
- 1:38
the home component is still there such
- 1:39
as the logo and header that'd be a good
- 1:41
place to put a nav bar at a footer and
- 1:43
things that'll be present on every page
- 1:45
that's a good start but let's look at
- 1:47
how we can navigate between multiple
- 1:48
components by generating another one
- 1:50
called animals then we can import the
- 1:53
component into our router module and set
- 1:55
up another path and component property
- 1:57
for this route when setting up
- 1:59
hyperlinks in angular you don't use a


