在Angular 4中自动将一个页面导航到另一个页面

问题描述 投票:0回答:2

我正在开发Angular 4框架中的在线游戏。

预载完成后,页面应重定向到我的游戏页面。但是我不知道如何在Angular 4中自动重定向。你能不能用任何人解释一下它对我来说会更好。

提前致谢!!!

angular navigation
2个回答
0
投票

使用Router.navigate()并在预加载完成后导航到不同的页面

export class Preloader {
      constructor(private router: Router) {
        this.preloadItems();
      }

      preloadItems() {
       // once preload done
       this.router.navigate(['/your_route_path']);
      }
    }

0
投票

根据我的理解,如果你想在完全加载页面后重定向你的页面,你可以使用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']);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.