Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用角度2在n秒后自动重定向_Angular_Angular2 Routing - Fatal编程技术网

Angular 使用角度2在n秒后自动重定向

Angular 使用角度2在n秒后自动重定向,angular,angular2-routing,Angular,Angular2 Routing,我想显示一个页面“n”秒,然后重定向到另一个路由 在Angular 1.x中遇到了一些关于n秒后自动重定向的stackoverflow帖子。但我不知道如何在Angular2中实现同样的功能?您可以从@angular/Router中插入并使用路由器,并在设置超时中导航 import { Router } from '@angular/router'; constructor(private router: Router) {} ngOnInit() { // do init at he

我想显示一个页面“n”秒,然后重定向到另一个路由


在Angular 1.x中遇到了一些关于n秒后自动重定向的stackoverflow帖子。但我不知道如何在Angular2中实现同样的功能?

您可以从
@angular/Router
中插入并使用
路由器
,并在
设置超时
中导航

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

constructor(private router: Router) {}

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

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

它有点粗略,但这将工作

    setTimeout(() => {
          setTimeout(() => {
            this.router.navigateByUrl("/home");
          });
        }, 3400);
      }

window.setTimeout
,然后像在angular2中通常那样重定向。如果您需要更多帮助,请显示您目前拥有的代码。只是为了明确路由器对象应该被注入构造函数seems中,这与上面接受的答案类似。。i、 e.使用setTimeout。您也不需要设置2个超时。