Auto redirecting after n seconds using Angular 2

前端 未结 2 1189
醉梦人生
醉梦人生 2021-02-08 12:11

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

相关标签:
2条回答
  • 2021-02-08 12:35

    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
    }
    
    0 讨论(0)
  • 2021-02-08 12:39

    its a-bit sketchy but this will work.

        setTimeout(() => {
              setTimeout(() => {
                this.router.navigateByUrl("/home");
              });
            }, 3400);
          }
    
    0 讨论(0)
提交回复
热议问题