简体   繁体   中英

Redirect on ngOnInit Angular 2

I'm trying to redirect my app when launching and it's not working. I'm just trying to use router.navigate in my ngOnInit() and it returns false.

I tried to insert it in a setTimeout and then it works.

If you have any idea it would be appreciated :)

ngOnInit() {

    // On redirige au lancement sur la page hors connexion
    setTimeout(() => this._router.navigate(["/hors-connexion"]));

}

I would try it like this:

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

@Component({
    selector: 'test-component',
    template: '<ng-content></ng-content>'
})

export class TestComponent {

    constructor(
        public router: Router
    ) { }

    ngOnInit() {

        this.router.navigate(['hors-connexion']);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM