38

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

Sunjay Varma
  • 5,007
  • 6
  • 34
  • 51

4 Answers4

51

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");
  }
}
Jesse
  • 3,522
  • 6
  • 25
  • 40
Daniel Camarda
  • 1,126
  • 12
  • 18
  • 1
    this.storage is definetly not needed (copy-paste from some of my code) i just tested it without the hash and it seems to work but generating a slightly different hash in the url... "/myroute" gives you http://mydomain/#myurl while "#/myroute" generates http://mydomain/#/myurl but i think there is no need for the '/' at the beginning so i'm removing it from the code :) – Daniel Camarda Mar 02 '15 at 03:58
  • @DanielCamarda Hi Daniel, is the exception thrown when calling it 'router' a bug? I was experiencing the same thing and it was driving me mad. EDIT: Just spotted the post below by Rob. – mbx-mbx Mar 09 '15 at 12:28
  • yeah :) just edited the answer to add @EisenbergEffect reference to the bug – Daniel Camarda Mar 09 '15 at 18:54
  • @SunjayVarma "The this.storage line probably shouldn't be there. " This is confusing. For me I needed a this. for the router to be accessible to the class methods. – CSSian May 10 '15 at 23:01
  • @KevinM I deleted that comment. It was in reference to some code that was there which Daniel then edited out. – Sunjay Varma May 11 '15 at 18:04
  • In the latest version this method is changed to `navigateToRoute`. – Suhas Jan 01 '16 at 16:47
21

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");
  }
}
jeff-h
  • 2,184
  • 1
  • 22
  • 33
17

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.

EisenbergEffect
  • 3,989
  • 22
  • 26
8

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')
}
specimen
  • 1,735
  • 14
  • 23