concept route in category aurelia

This is an excerpt from Manning's book Aurelia in Action.
Figure 1.9. Zooming in on the routing component of your Aurelia map, the router works by looking up a URL (in the case of the example, the /about URL) and matching it to a route found in the route dictionary. The route dictionary determines which view-model to load. In this case, you’ve specified that you want the about view-model to load whenever a user visits the #/about URL.
![]()
One of the tools required in most SPAs today is routing. Routing provides a way of mapping URLs to application routes. The benefit is that you can build an SPA in a way that makes it feel like a real website, so conventions such as the Back button returning to the previous page work like the user expects. As mentioned, taking advantage of a routing tool in your SPA allows you to implement deep linking, which allows users to visit a path deep within your application (for example, /products?id=1) and have the page rendered with the state that they expect (in that case, the product with the given ID). Aurelia lets you configure an array of URLs called routes. When you navigate to a route, it looks up the URL entry in the dictionary and finds the view-model that corresponds to this route. It then constructs that view-model on your behalf. But how do you get from that constructed JavaScript class to something that’s rendered on the page? Figure 1.9 gives you a vital clue, outlining how a typical routing setup in Aurelia works.
Figure 1.9. Zooming in on the routing component of your Aurelia map, the router works by looking up a URL (in the case of the example, the /about URL) and matching it to a route found in the route dictionary. The route dictionary determines which view-model to load. In this case, you’ve specified that you want the about view-model to load whenever a user visits the #/about URL.
![]()
A URL change; for example, when a user navigates to a page within your application (such as by using the route #/books/edit/1). In this case, you’d need something to listen for, either the hashchange DOM or a history event such as pushstate. You can read more about the hashchange event on MDN at https://developer.mozilla.org/en-US/docs/Web/Events/hashchange.
Besides the route, name, and moduleId properties (configured in chapter 2), the routing configuration has several more properties available. You’ll configure two of these properties now. The title property determines the web-page title shown in the tab (for example, home | my-books). You’ll also repurpose this property to display the title of the current route in the navigation view.
The nav property defaults to false, and routes can be marked as navigable by setting this property to true. This value is used by the router.navigation property, which returns a list of the navigable routes (routes enabled for display on the navigation menu). The settings property can be used to provide additional data to your Aurelia route. In this case, use it to indicate an optional Font Awesome icon that should be displayed along with the menu item. To demonstrate this, add a console log message to display a value for this property.
Listing 9.2. nav properties added to the route configuration (app.js)
export class App { configureRouter(config, router) ... config.map([ { route: ['', 'home'], name: 'home', moduleId: 'index', title:'home', nav:true , settings: {icon:'home'}}, { route: 'books', name: 'books', moduleId: './resources /elements/books', title:'books', nav:true, settings: {icon:'book'}}, ]); } }Reload the browser to see the newly configured routing rules. If you open the developer tools window, you should see the navigable routes exported to the console. Figure 9.4 highlights the isActive, title, and settings properties available in the NavModel object. With the routing in place, the next step is to create a new HTML-only custom element and render the routes to the page.
Redirects allow you to specify that when a user visits a route, they should be redirected to another route. These are particularly useful for when you want to retire a route in your application without breaking the user’s existing bookmarks. You can do this by keeping the old route in your route array (the from route) and adding a redirect route that should be returned instead (the to route).
To see how this is used, modify the my-books route configuration again to add support for a legacy version of the users route. Modify the route configuration in the ./src/app.js file to add a new 'legacy-users' route. This route will have a redirect: 'URL FRAGMENT' property that takes the URL fragment you want to redirect to (the users route). The route expression that you’ll use here is (route: 'legacy-users', redirect: 'users'). This expression explicitly looks for the route #/legacy-users and redirects it to #/users. You could make this route more flexible and allow it to also handle parameterized routes by adding a wildcard to the route expression (route: 'legacy-users/*'), which would also match URLs like #/legacy-users/bob/details.