To create a child router, create a new router on the child route in the same way as you created the router on the parent route. Then set the router as a property of the view model class so that you can access it throughout the view model.
import { Router } from 'aurelia-router'; class ChildViewModel() { configureRouter(config, router) { this.router = router;
If you want to use the child router in a different view model, you can import it and use the router in the class (assuming the class is singleton, which it should be, unless you configured it differently).
import { inject } from 'aurelia-framework'; import { ChildViewModel } from './child-view-model'; @inject(ChildViewModel) class AnotherViewModel() { constructor(cvm) { this.externalRouter = cvm.router; } doStuff() { this.externalRouter.navigate('somewhere'); } doOtherStuff() { ChildViewModel.router.navigate('somewhere else'); } }
source share