我有一个“验证”方法可以让我登录。我真的不知道它是否有效,因为我使用虚拟登录,只是为了测试路由和返回日期。 这种方法的问题是,在成功登录后,我应该被重定向到 Actuals 页面,目前该页面不起作用。
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”。从我读到的内容中我可以猜到这个页面不是实际页面的父页面。我在另一篇文章中尝试过这个建议:this.router.parent.navigate(['/Actuals']); 那是在帖子中:Angular 2 - How to navigate to another route using this.router.parent.navigate('/about')这在我的情况下不起作用,不知道为什么......
最佳答案
您需要注入(inject)路由器以使其可用
constructor(private:Router router) {}
关于Angular 2 - 成功登录后重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479144/