| Start |
|---|
| Controlling Access to or from a Route The default Angular router allows navigation to and from any route unconditionally. This is not always the desired behavior. In a scenario where a user may conditionally be allowed to navigate to or from a route, a Route Guard may be used to restrict this behavior. If your scenario fits one of the following, consider using a Route Guard,
How Route Guards workRoute Guards work by returning a boolean value to control the behavior of router navigation. If true is returned, the router will continue with navigation to the target component. If false is returned, the router will deny navigation to the target component. Route Guard InterfacesThe router supports multiple guard interfaces:
These interfaces can be implemented in your guard to grant or remove access to certain processes of the navigation. Synchronous vs. Asynchronous Route GuardsRoute Guards allow synchronous and asynchronous operations to conditionally control navigation.
Synchronous Route GuardA synchronous route guard returns a boolean, such as by computing an immediate result, in order to conditionally control navigation. Asynchronous Route GuardFor more complex behavior, a route guard can asynchronously block navigation. An asynchronous route guard can return an Observable or Promise. This is useful for situations like waiting for user input to answer a question, waiting to successfully save changes to the server, or waiting to receive data fetched from a remote server. |
| Add guard to route configuration File app.routes Protected routes have Export the APP_ROUTER_PROVIDERS to be used in app bootstrap |
| Using Resolvers and Guards We're using a toplevel guard in our route config to catch the current user on first page load, and a resolver to store the value of the A simplified version of our implementation looks as follows: Here is our top level route: Here is our Here is our Here is our |
| Use Guard in app bootstrap File main.ts (or boot.ts) Consider the examples above:
|
| Bootstrapping Now that the routes are defined, we need to let our application know about the routes. To do this, bootstrap the provider we exported in the previous example. Find your bootstrap configuration (should be in |
| Configuring router-outlet Now that the router is configured and our app knows how to handle the routes, we need to show the actual components that we configured. To do so, configure your HTML template/file for your top-level (app) component like so: The For example: Lets say you wanted a a toolbar on every page that stays constant between routes, similar to how Stack Overflow looks. You can nest the |
| Changing routes (using templates & directives) Now that the routes are set up, we need some way to actually change routes. This example will show how to change routes using the template, but it is possible to change routes in TypeScript. Here is one example (without binding): If the user clicks on that link, it will route to Here is an example with data binding: Which would require an array called This will loop through the array and add an This is particularly helpful if you have a lot of links, or maybe the links need to be constantly changed. We let Angular handle the busy work of adding links by just feeding it the info it requires. Right now, |
| Setting the Routes NOTE: This example is based on the 3.0.0.-beta.2 release of the @angular/router. At the time of writing, this is the latest version of the router. To use the router, define routes in a new TypeScript file like such In the first line, we import
Then, export the array of routes.
Finally, we export the configured router. RemarksThere are a few more tricks we can do with the router (such as restricting access), but those can be covered in a separate tutorial. If you need a new route, simply modify
|
Read Article
https://develop.horje.com/learn/1434/reference