4

I have a method "authenticate" that lets me login. I don't really know if it works because I use dummy login, just to test the routing and returned date. The problem with this method is that on successful login I should be redirected to the Actuals page, which at this moment doesnt work.

authenticate(username, password) {
var creds = JSON.stringify({ UserName: username.value, UserPassword: password.value, SetDebug: true});
var headers = new Headers();
headers.append('Content-Type', 'application/json');

      this.http.post('http://demo/aariworx5/api/login/login', creds ,{headers: headers})
          .subscribe(
          res => {
              this.data = res.json();
              this.router.parent.navigate('/Actuals');
          },
          error => console.log(error)
          );
  }
}

The error I get is the following: TypeError: Cannot read property 'parent' of undefined. I can guess from what I read that this page is not the parent of the Actuals page. I have tried the suggestion in another post: this.router.parent.navigate(['/Actuals']); That was in the post: Angular 2 - How to navigate to another route using this.router.parent.navigate('/about') This does not work in my case, don't know why...

Community
  • 1
  • 1
JanVanHaudt
  • 253
  • 1
  • 6
  • 16

1 Answers1

2

You need to inject the router to have it available

constructor(private:Router router) {}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I had public router: Router, I thought this worked? btw, the suggestion does not seem to work and gives the same error – JanVanHaudt Feb 18 '16 at 10:43
  • Obviously the `this.router` is `undefined` and `undefined` doesn't have a `.parent`. So the question is why is `router` undefined? But that's hard to tell from your question because it doesn't contain the relevant code. – Günter Zöchbauer Feb 18 '16 at 10:45
  • To my shame I overlooked the fact that I had the router in component but not in the service. I thank you for your help, as it now works as it should. – JanVanHaudt Feb 18 '16 at 10:55
  • 2
    Can you please accept the answer to indicate your problem is solved? (using the checkmark below the up/down-vote button. – Günter Zöchbauer Feb 18 '16 at 10:58