我正在开发Angular 4框架中的在线游戏。
预载完成后,页面应重定向到我的游戏页面。但是我不知道如何在Angular 4中自动重定向。你能不能用任何人解释一下它对我来说会更好。
提前致谢!!!
使用Router.navigate()并在预加载完成后导航到不同的页面
export class Preloader {
constructor(private router: Router) {
this.preloadItems();
}
preloadItems() {
// once preload done
this.router.navigate(['/your_route_path']);
}
}
根据我的理解,如果你想在完全加载页面后重定向你的页面,你可以使用ngAfterViewInit
方法,如下所示:
// Import AfterViewInit
import { Component, OnInit, AfterViewInit} from '@angular/core';
export class YourComponent implements OnInit, AfterViewInit {
// YOUR CODE
constructor(private _router: Router) {
};
ngAfterViewInit() {
this._router.navigate(['Your_router_link']);
}
}