Angular 2-成功登录后重定向不起作用 [英] Angular 2 - Redirect after successful login not working
问题描述
我有一个身份验证"方法,可以让我登录.我真的不知道它是否有效,因为我使用了虚拟登录,只是为了测试路由和返回日期. 这种方法的问题在于,成功登录后,我应该重定向到实际"页面,该页面目前不起作用.
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)
);
}
}
我得到的错误如下:TypeError:无法读取未定义的属性"parent".从我读到的内容中可以推测出,该页面不是Actuals页面的父页面.我在另一篇文章中尝试了这个建议:this.router.parent.navigate(['/Actuals']); 那是在帖子中:角度2-如何使用this.router.parent.navigate('/about')导航至另一条路线
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...
推荐答案
您需要注入路由器以使其可用
You need to inject the router to have it available
constructor(private:Router router) {}
这篇关于Angular 2-成功登录后重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!