Philip John
Philip John

Reputation: 5565

Auto redirecting after n seconds using Angular 2

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?

Upvotes: 16

Views: 28659

Answers (2)

Alex Richkov
Alex Richkov

Reputation: 17

its a-bit sketchy but this will work.

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

Upvotes: 0

Pengyy
Pengyy

Reputation: 38191

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
}

Upvotes: 44

Related Questions