- Overview
- Transcript
6.1 Defining and Configuring Routes
We'll start this chapter by looking at how we can define the routes that, together with our components, make up the states of the example application.
1.Introduction6 lessons, 42:00
1.1Introduction00:48
1.2Get Started With Angular-CLI11:09
1.3Developing With Angular-CLI13:17
1.4TypeScript vs. JavaScript06:54
1.5Angular Modules From the CLI04:31
1.6CLI Options05:21
2.Get Started With Angular7 lessons, 42:38
2.1Bootstrapping the Application04:30
2.2The Application Module04:15
2.3The Application Component08:06
2.4Component Styling03:06
2.5Global Styling05:11
2.6Creating a Component With the CLI09:34
2.7Creating a Service With the CLI07:56
3.Core Concepts7 lessons, 55:20
3.1Component Trees06:20
3.2Dependency Injection06:52
3.3Content Projection05:38
3.4Component and Directive Lifecycle Methods06:31
3.5Component-Only Lifecycle Methods05:28
3.6Decorators07:36
3.7Models16:55
4.Template Deep Dive11 lessons, 1:10:56
4.1Basic Data Binding With Interpolation05:35
4.2Property Bindings07:07
4.3Attribute Bindings03:29
4.4Event Bindings08:16
4.5Class and Style Bindings05:44
4.6The `NgClass` and `NgStyle` Directives05:04
4.7The `*ngIf` Directive04:41
4.8The `*ngFor` Directive09:29
4.9Inputs05:33
4.10Using Pipes in a Template07:31
4.11Using Pipes in a Class08:27
5.Forms10 lessons, 1:45:41
5.1Handling User Input With Template Reference Variables07:06
5.2Template-Driven Forms11:10
5.3Template-Driven Forms: Validation and Submission14:00
5.4Reactive Forms11:26
5.5Using a `FormBuilder`08:01
5.6Reactive Validation With Built-in Validators14:53
5.7Creating Custom Validators for Template-Driven Forms12:18
5.8Creating Custom Validators for Reactive Forms08:26
5.9Observing Form State Changes12:40
5.10Working With the `@HostListener` Decorator05:41
6.Routing9 lessons, 1:15:10
6.1Defining and Configuring Routes07:53
6.2Rendering Components With Router Outlets10:14
6.3Using Router Links for Navigation05:25
6.4Navigating Routes Using the Router06:24
6.5Determining the Active Route Using an Activated Route07:16
6.6Working With Route Parameters10:42
6.7Using Route Guards07:36
6.8Observing Router Events10:55
6.9Adding Child Routes08:45
7.Using the HTTP Client5 lessons, 56:24
7.1Sending an HTTP Request10:52
7.2Handling an HTTP Response11:22
7.3Setting Request Headers12:33
7.4Intercepting Requests09:04
7.5Finishing the Example Application12:33
8.Testing10 lessons, 1:23:27
8.1Service Unit Test Preparation10:45
8.2Unit Testing Services13:24
8.3Component Unit Test Preparation12:35
8.4Unit Testing Components07:27
8.5Unit Testing Component Templates06:58
8.6Unit Testing Pipes04:41
8.7Unit Testing Directives04:56
8.8Unit Testing Validators04:48
8.9Unit Testing Observables11:37
8.10Unit Testing HTTP Interceptors06:16
9.Building for Production1 lesson, 03:40
9.1Building for Production03:40
10.Conclusion1 lesson, 01:32
10.1Conclusion01:32
6.1 Defining and Configuring Routes
HI folks, in this lesson we're going to begin looking at routing. Routing is a familiar concept for single page applications, and is a combination of a URL and a piece of view. Typically, in a modern JavaScript application, we don't want the whole application to reload every time the user wants to view different parts of the application. So routing allows us to update the view of our application, perhaps taking the user to an entirely different part of the application without the browser reloading and losing the current state. So let's think about this for our example application. At the moment, we have some different states for our game. There is the initial start page, where the user enters their name and chooses some game options. Then there is the actual game screen, where the user will play the game. And lastly, there is the end screen, where the player will see the result of the game and can quit or choose to play again. We are controlling access to these different states using simple ng ifs to show or hide the different components. For our needs this is fine, we have a limited number of views to present and a limited number of components we want to navigate between. But as an application grows, using these ng iffs like we are in the example application will quickly become burdensome. For larger applications with many more views and states, we will likely want the more elegant solution with less complexity, and routing can be that solution. So even though we don't strictly need to, we're going to spend the next few lessons implementing routing in the example application. So let's make a start. In larger applications, people will generally create a separate routing module that contains all of the routing logic, we're not going to go that far. A single file containing all of our routes will be more than adequate. So the first step in setting up routing will be for us to define our routes. Let's create a new file in the root of our application called app.routes.ts. We'll only need one import from Angular So we're going to import the routes type from angular/router, but we will also need to import some of our own components, the ones that we would like to route to. These are the component will be routing between in the example application. The app.routes.ts file that we've just created will export the routes that we define. So we're going to export an array of the type routes with the identifier appRoutes. So each root will be an object that we use to define information about the route. One route will be the StartComponent. A route at a minimum will contain a path which is the URL that will be used to match the route and a component which is the component we want to route two when the path is matched. We can also add routes for the game and end components. So we've defined routes for our three main components. Another route we can define is an empty route. This serves as the default route. For us, the start component could be seen as the default component. So let's add a default route that points back to the start component. This route has no path, because it is the default route and serves as the entry point to the application. Which is typically at the route of the app, and not behind a deeper URL. We already have a route that renders the start component. So instead of defining the startComponent as the component we want to route to for the default component, we can just redirect to the /startRoute. The path match option here ensure that this route is only matched if navigating to slash the route URL. One question remaining, at this point, is what happens if someone tries to route to a URL that doesn't exist? To handle this scenario, there is one more route we need to define, a wildcard route. That we can use to redirect people if they try to hit a route that does not exist. For this let's add a new component, a page not found component, let's create one with the CLI. Now we can import this new component into our roots files as well. And then lastly we can add the wildcard route that will match any unspecified URL and redirect to this page not found component. The wildcard route should always be defined last. It's the last route that will be hit if no other routes are matched. One key point, which we don't have to worry about because the CLI automatically added it for us. Way back at the start of the course when we generated the initial application, it's the base href. Let's just open up the index.html file in the root of the app here. And we can see that there is this base element with the href pointing to slash. So this is generally what we would set it to as the route URL for the app. Angular needs this for any routing to work. So if the CLI hadn't added it for us, we would have to add it ourselves manually. Obviously, the CLI did add it for us, so we don't need to do anything here. But you should note this for future applications that you might build. So in this lesson we've learned a little bit about the purpose of routing and we've seen that we can define the routes for our application using a simple array of objects. Where each object represents a single route with a defined path which maps to a component. We saw how to define a default empty route for our application's initial landing page. And we also saw how to define a wildcard route that can be used to route to a page not found component if no other routes are matched. Thanks for watching.