In v1, it was possible to specifiy routes dynamically based on dependencies like:
@autoinject
export class MyComponent {
constructor(
private permissions: Permissions)
{
}
async configureRouter(config: RouterConfiguration, router: Router)
{
if (this.permissions.feature1)
{
routes.push({
route: 'route1',
name: 'route1',
moduleId: PLATFORM.moduleName('./component1')
});
}
if (this.permissions.feature2)
{
routes.push({
route: 'route2',
name: 'route2',
moduleId: PLATFORM.moduleName('./component2')
});
}
config.map(routes);
config.mapUnknownRoutes(routes[0]);
}
}
Is there still a possibility to do that in Aurelia 2? Creating routes using a static property or by using the route decorator doesnβt seem to work for this scenario?
Thanks for any help.
AFAIK this kind of conditional route configuration is not supported. However, you can use the routing hooks as described here: https://docs.aurelia.io/developer-guides/routing/router-hooks#authentication-using-lifecycle-hooks
Thanks for your feedback. Isnβt it a bit too simple if routes can only be defined statically? I can imagine many different scenarios where routes depend on dynamic state, especially in larger business applications.
If your need is simply limited to authorized routes then, as I said before, you can use the hooks. Otherwise, for more dynamism, you may use the GitHub - jwx/aurelia-direct-router .
OK, thanks for your help!
1 Like
@Sayan751 I thought the direct router was meant to be v2 core again?
jwx
March 28, 2022, 10:18pm
7
It is, itβs just not there yet. Iβm working on it and hope to present news soon. (The direct router supports dynamically configuring routes.)
2 Likes
Looks like this is what I need!
aurelia:master
β aurelia:topic/router-lite-configuration-hook
opened 06:08PM - 28 May 22 UTC
<!---
Thanks for filing a pull request π ! Before you submit, please read the fβ¦ ollowing:
Search open/closed issues before submitting since someone might have pushed the same thing before!
-->
# Pull Request
## π Description
<!---
Provide some background and a description of your work.
-->
This PR adds support for an instance (non-static) hook to configure routes. This can be thought as the parallel of `configureRouter` in v1. The difference is in the signature and obviously the schema of the routing configuration object.
```typescript
getRouteConfig(parentDefinition: RouteDefinition | null, routeNode: RouteNode | null): IRouteConfig;
```
This means that the following two routing configurations are synonymous.
```typescript
@route({
routes: [
{ path: 'c1', component: C1 },
{ path: 'c2', component: C2 },
],
})
```
```typescript
public getRouteConfig(def: RouteDefinition | null, node: RouteNode | null): IRouteConfig {
return {
routes: [
{ path: 'c1', component: C1 },
{ path: 'c2', component: C2 },
],
};
}
```
Obviously, the non-static infra adds the flexibility of dynamically configuring the routing table, like v1.
Additionally, it sets the default transition plan to `replace` as the `invoke-lifecycles` plan often causes unexpected surprises.
### π« Issues
<!---
* List and link relevant issues here.
-->
## π©βπ» Reviewer Notes
<!---
Provide some notes for reviewers to help them provide targeted feedback.
-->
Note that the `@route` decorator can be deprecated. However, we might need to think it over, whether we want to do that. The decorator still offers less verbosity. And sometimes, if the routing needs are not that critical, static routes might work as well. I am not saying we should not remove that, because removing will also result in removing some related infra. I am suggesting to postpone the removal of the decorator to a later point, after the things are discussed.
## π Test Plan
<!---
Please provide a summary of the tests affected by this work and any unique strategies employed in testing the features/fixes.
-->
## β Next Steps
<!---
If there is relevant follow-up work to this PR, please list any existing issues or provide brief descriptions of what you would like to do next.
-->
<!--
Love Aurelia? Please consider supporting our collective:
π https://opencollective.com/aurelia
-->
1 Like