Routing is component-based, so one should identify the set of components which provide as routing targets and define a route for each one. The snippet code below demonstrate how the route configuration is done.
app.module.ts
[c]
...
import { RouteModule } from '@angular/router';
@NgModule({
import: [
BrowserModule,
FromsModule,
HttpModule,
RouterModule.forRoot([], { useHash: true})
],
declarations: [
...
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
[/c]
An Angular application has one router that is managed by Angular's router service and before using that service, one need to register the service provider in an Angular module, similar to the http module, Angular provides a
RoutherModule in the Angular router package that registers the router service provider. To include the features of this external module in the application, one need to add it to the imports array of the application's Angular module. In addition to registering the router service provider, the RoutherModule also declares the router directives.
By importing the RoutherModule, the component templates can use the router directive. RoutherModule also exposes the routes one configured. Before navigating to a route, one need to ensure that the routes are available to the application and this can be done by passing the routes to
RoutherModule as shown below.
RouterModule.forRoot([])
Call the
RoutherModules forRoot method and pass the array of routes to that method. This establishes the routes for the root of the application. If one want to use
hash style routes instead of html
five style routes, change the code to set useHash as show.
RouterModule.forRoot([], { useHash: true})
Finally, one can now configure some routes.
The router must be configured with a list of route definitions. Each definition specifies a route object. Each route requires a path. The path property defines the URL segment for the route. When the route is activated, the URL path segment is appended to the URL of the application. The user can type in or bookmark the resulting URL to return directly to the associated components view. In most cases one can also specify a component, which is the component associated with the route.
The above image give the examples of route definitions. The first route simply maps a specific
URL path segment to a specific component. So the URL displays the template from the Product List Component.
The
colon ID in the second route represents a route parameter. The Product Detail Page displays the detail for one product so it needs to know which product to display. The Product Detail Component reads the ID from the path segment and displays the defined product. One can define any number of parameters separated wit slashes.
The third route display the template from the
Welcome Component using the URL.
The fourth route defines the default route. The redirect translates the empty route to the desired default path segment. In this example, the
welcome route. A redirect route requires a path match property to tell the router how to match the URL path segment to the path of a route. This default route is only used when the entire client side portion of the path is empty, so set the path match to full.
The
asterisks(**) in the last route denote a wildcard path. The router matches this path if the requested URL doesn't match any prior paths defined in the configuration. This is useful for displaying a 404 Not Found Page or redirecting to another route.
A few things to note in all of this route definitions. There are no leading slashes in the path segments and the order of the routes in this array matters. The router uses a first match win strategy when matching the routes. This means that more specific routes should always be before less specific routes, such as the wildcard route.