From the course: Learning the Angular CLI

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Generating route guards

Generating route guards - Angular Tutorial

From the course: Learning the Angular CLI

Start my 1-month free trial

Generating route guards

- [Instructor] In Angular applications, we can protect routes using what is called a route guard. A route guard is used to perform certain checks when the route is being requested, and it can decide whether or not to continue with the navigation. In our particular case, we wish to lock certain routes, so that the user can't see data on this data logged in. Let's generate a new guard using the CLI; ng generate guard, and we will call this route guard auth. It will ask us which interface we would like to implement. For this example, we will choose the CanActivate guard, which moderates a navigation to a route. If true is returned, then it will continue navigation to the requested route. If false is returned, it will stop navigation. Now in the app routing module, we will first need to import this guard; import from /auth.guard. And let's go ahead and import our guard here. Then for the routes you want to protect, we can…

Contents