Auto redirecting after n seconds using Angular 2

﹥>﹥吖頭↗ 提交于 2020-01-12 05:48:06

问题


I wanted to display a page for 'n' seconds and then redirect to another route.

Came across a couple of stackoverflow posts (url1 and url2) about auto redirecting after 'n' seconds in Angular 1.x. But I m confused how to implement the same in Angular2?


回答1:


You can inject and use Router from @angular/router and navigate in setTimeout.

import { Router } from '@angular/router';

constructor(private router: Router) {}

ngOnInit() {
    // do init at here for current route.

    setTimeout(() => {
        this.router.navigate(['nextRoute']);
    }, 5000);  //5s
}


来源:https://stackoverflow.com/questions/42488068/auto-redirecting-after-n-seconds-using-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!