【发布时间】:2017-08-23 23:43:44
【问题描述】:
当我的 Angular 2 (v2.4) 应用程序加载时,我试图将用户重定向到某个页面,而不管他们输入的 URL 是什么。例如,如果用户使用http://www.myapp.com/somepage 或http://www.myapp.com/anotherpage 加载应用程序,应用程序将自动重定向到http://www.myapp.com/entrypage。
为此,我的 AppComponent 中有以下代码(这是第一个加载的组件):
export class AppComponent implements OnInit {
constructor(private router: Router) {
}
ngOnInit() {
console.log(this.router);
this.router.navigateByUrl("/", { skipLocationChange: true });
}
}
但是,当应用加载时,重定向不起作用。它似乎在返回原始请求的路线之前快速重定向。
我怎样才能让它工作?有没有一种“正确”的方法可以让它发挥作用?
JT
【问题讨论】:
-
试试下面的代码。 this.router.navigate("/", { skipLocationChange: true });