I will try to answer your questions one by one below.
I will start with Q2
Q.2: Why do we need to keep the router here if it is always accessible from the Aurelia router?
So, in your App App-View App Class, you refer to the router property in your view: <nav-bar router.bind="router"></nav-bar> , so you need to declare the property to use it. In the second view, you don’t need you like that :-)
The router property is also added when you need to do something with the router in main.ts / main.js - the starting point of your application. This is because the router is configured for the first time there, and the injection will not work in the constructor, so you need to save this property in order to get it in the configureRouter(..) method (note: this was before beta 1, t know if he is still there now).
In your code, you have a call for this.router.refreshNavigation(); This will ensure that your router is updated with new routing location information.
Q.3: I saw both router.navigateToRoute and router.navigate links, but there is no indication when to use either what the difference is, and the document did not see to explain. What should i use? Documentation
The router.navigate(fragment: string, options?: any) method uses a fragment of the URL and not the name of the route for navigation, for example, for example. router.navigate('#/app/child', {...<options - not params od the URL>...}) . This method should be used to navigate absolutely between routers, as well as to access the parent URL, etc.
If you are just navigating the current router, you will always use router.navigateToRoute(route: string, params?: any, options?: any) . This method uses the name of the route, not the URL, and we simply specify the name of the route on the user routing map (custom means the current routing map of the child or the current main routing relative to the location of the URL that we find on the page). Here you can pass the URL parameters in a more convenient way, as you can see. You can use the params object instead of concatenating the URL with parameters.
Q.4: Again, I want the routing to be relative, even on an HTML page. The above will not work, so what should I use?
In Aurelia, we do not use the href attribute of the a tag directly for navigation. As Brandon already answered, you should use the route-href attribute, which is probably not documented anywhere, just appears on forums and portals. This is equivalent to router.navigateToRoute(route: string, params?: any, options?: any) , so you cannot use it to navigate between routers, in this case you can use your own attribute or just use click.triger="navTo('#/app/child')" , where the navTo() method is implemented in your View model and looks like this:
public navTo(routeName: string) {
And finally, your question:
Q.1: How to navigate between child routes
You probably know the answer now, just use: router.navigate(fragment: string, options?: any) with an absolute URL.
The following is an example of a custom attribute to solve this problem:
import {inject} from "aurelia-dependency-injection"; import {Router} from "aurelia-router"; import {customAttribute} from "aurelia-framework"; @customAttribute('nav-href') @inject(Element, Router) export class NavHref { private value: string = ''; constructor(private element: Element, private router: Router) { let $this = this; element.addEventListener('click', () => { if ($this.value === 'back') { $this.router.navigateBack(); } else {
First you need to import it into your HTML, I named my nav.href.ts file:
<require from="common/nav.href"></require>
Then just use it in your HTML code:
<a nav-href="#/home/any-location">My link to any location</a>
Hope this helps you, welcome :-)