Redirecting Users
Explore how to implement user redirection in Angular by injecting the Router, using the navigate method, and adjusting tests to mock routing. Understand how to redirect users to a dashboard view and ensure your tests pass smoothly by handling Router dependencies.
We'll cover the following...
We'll cover the following...
Now that our new routing has been set up, we can redirect our users to this new view. First, we’re going to add this redirect to our component. Then we’ll update our tests for this new functionality.
Use the updated code below to make the changes instructed in the lesson.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>LetsGetLunch</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> <app-root></app-root> </body> </html>
Dashboard component
Update signup component
Follow the steps below to redirect our users:
- Import Angular’s
Router. - Inject the router into our component’s constructor.
- Remove the comment about redirecting users and utilize the
routerto navigate the users to the newly created dashboard. Here, callrouter.navigate(), passing it an array that contains the value of the path