configureRouter requires child routes

I made a change to a component to add configureRouter() to grab the router object.

  configureRouter(config: RouterConfiguration, router: Router) {
    this.router = router;
  }

When navigating to that component, however, I got a Route not found: error and navigation was halted. I’m sure this is the expected behavior, but from the documentation one would never know that.

Is there clarification on this behavior? Also, how to grab the router in a component if configureRouter() can only be implemented on root components?

You can inject the AppRouter via DI to get hold of it. How to use navigateToRoute - #5 by jeffgrann

ok, that’s interesting. So, does that mean that all navigation paths using that method is from the root of the app, not relative to the component getting the injection?

For example, I would have to navigate using “/profile/settings” from the profile root component, and not “/settings”?

I think the difference is whether you inject childrouter or router itself though I’m not sure. Try searching discourse I think I remember something with regards to this once explained

There are two classes from aurelia-router you can inject,

Router, you will get the current router instance as you questioned. There is api to get to parent router from current router.

AppRouter, this ensures you get the root router of your app. In fact, AppRouter is a subclass of Router.

2 Likes