简体   繁体   English

重定向ngOnInit Angular 2

[英]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. 我只是想在ngOnInit()中使用router.navigate,它返回false。

I tried to insert it in a setTimeout and then it works. 我试图将其插入setTimeout中,然后工作。

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']);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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