Route parameters, determineActivationStrategy and configureRouter in Aurelia 2?

I’ve been looking at the documentation of Routing in Aurelia 2 and also found this migration guide;
https://docs.aurelia.io/resources/cheat-sheet#migrating-from-v1-3

However, nowhere are route parameters mentioned. In my current app I just have a few route “patterns” set up with parameters annotated with colon, for example
route: ':controller/:action/:id?/'

In Aurelia 1 the colons means that there are three parameters; controller, action and id. And the question mark means that id is optional.

Is this possible to use in the new “path” property of Aurelia 2 routes?


Also, the migration docs does not mention anything about how to migrate the determineActivationStrategy method that exists in Aurelia 1.


Lastly, my current configureRouter method depends on some data that I’m injecting to my app class.
But now when the route configuration is to be done through a decorator on the App class, does it mean that all data for the routing must be constant or could I somehow call a method and build up my routes array using logic as I can in configureRouter of Aurelia 1?

@route({ routes: MyApp.buildRouteArray() })
export class MyApp {
    public static buildRouteArray() { 
      let data = aurelia.container.get(MyAppDataModel); //get my data object from DI container
     /.../ //build route array
   }
}

Any input on whether this or something like it will possible or not would be much appreciated.

1 Like

Yes the syntax for path in v2 supports all the v1 stuff too.

That’s renamed to transitionPlan which can be 'replace', 'invoke-lifecycles' or 'none', or a function returning one of those values. This is a property of the route config you pass into the decorator.

You could inject IRouteContext as a dependency in your component and then call .addRoute to add additional routes in the constructor. That route context is also where the static route config (that you added via the decorator or static routes) ends up. It’s the main entry point for component-scoped routing stuff (like child routers used to be in v1)

5 Likes

@fkleuver,

When I try to inject IRouteContext I get an error:
Error: Attempted to jitRegister an interface: IRouteContext

Just by chance I tried to inject RouteContext instead, but it also gives error:
TypeError: Cannot read property 'root' of undefined (at new RouteContext, route-context.ts:131)

Perhaps I should add that I’m trying to this in the constructor of my root component (the one passed to the app method of the Aurelia instance) and not to have any routes registered with the @route decorator at all.

You only need IRouteContext when you need to append routes to a component that already has routes. Since you don’t, you can just call the static Route.configure api like so:

Route.configure({
  routes: [
    { path: '', component: import('./home'), title: 'Home' }
  ]
}, App);

Which is functionally equivalent to doing this (these are just 2 different entry points to the same api):

@route({
  routes: [
    { path: '', component: import('./home'), title: 'Home' }
  ]
})
export class App {}

For future readers who do need the IRouteContext in the app root: a workaround is to retrieve it in the created hook, e.g.:

export class App {
  created(controller) {
    const routeContext = controller.context.get(IRouteContext);
  }
}

This workaround only applies to the app root and might be lifted in a future release, it’s due to a circular dependency that still needs to be addressed.

1 Like