I'm writing an application using Aurelia JS. How can I redirect to another URL? Is there a way to do it without creating a whole new navigation pipeline step?
Thanks
I'm writing an application using Aurelia JS. How can I redirect to another URL? Is there a way to do it without creating a whole new navigation pipeline step?
Thanks
to do that inject the router in the ViewModel and use the method navigate(route)
here is an example:
import {Router} from 'aurelia-router';
export class MyVM {
static inject() { return [Router]; }
constructor(router){
this.router = router;
}
someMethod(){
this.router.navigate("myroute");
}
}
Just thought I'd update @Daniel Camarda's excellent answer a little. As of Feb 2016 you can use Aurelia's inject decorator. Also, the router naming issue has been resolved.
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@inject(Router)
export class MyVM {
constructor(router){
this.router = router;
}
someMethod(){
this.router.navigate("myroute");
}
}
Quick bit of related info: The naming issue with "router" is something we are tracking here: https://github.com/aurelia/router/issues/34 If you are reading this answer later, and this issue has been closed, then you should be able to safely name your property "router" if you so choose.
Perhaps a slightly different use-case, but posting since it's so similar: Upon entering a view, based on e.g. parameters, I want to redirect to a different view. This must happen in activate() or canActivate().
In this case, both navigate() and navigateToRoute() suggested above does not work.
What does work however is return new Redirect('....') where Redirect is imported from aurelia-router, like this:
canActivate(param) {
if (param.id == null)
return new Redirect('/viewWhichDoesntNeedParam')
}