3

I use Angular 2, SystemJs and ES6 (Not TypeScript).

What do I want? I want navigate to link with Route. What I'm doing

// route for exam simple
export let routes = [
    {path: '', name: 'home', component: HomeComponent, terminal: true}, 
    {path: 'auth', name: 'auth', component: AuthComponent}
]

It works well if I use [routerLink]. And now I want programmatically use router like this

    import { Component } from '@angular/core';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'tf-main',
    directives: [ROUTER_DIRECTIVES],
    template: '<p>home</p><router-outlet></router-outlet>'
})

export class HomeComponent {

    static get parameters() {
        return [[Router]]
    }

    constructor(router) {
        this.router = router;

        // This wrong way!!!! Help!!!!
        this.router.navigateByUrl(['auth']);
    }
}

3 Answers 3

12

just

this.router.navigate(['auth']);

or

this.router.navigate(['/auth']);

(to ensure the root route auth is used)

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is this.router has not navigate method
What Angular2 version are you using?
I use Angular 2 version 2.0.0.rc4, router 3.0.0.beta1
5

How about using this constructor?

constructor(
private router: Router){}

then

ngOnInit() {
    this.router.navigate(['auth']);
}

I think your error message means you should initialize 'router' correctly.

Comments

1

According to me router.navigateByUrl accepts only string not array so your syntax should be like this

this.router.navigateByUrl('/auth');

or if this is your child route after home th

this.router.navigateByUrl('/home/auth');

see also

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.