【问题标题】:Redirect on ngOnInit Angular 2在 ngOnInit Angular 2 上重定向
【发布时间】:2017-02-14 07:03:19
【问题描述】:

我试图在启动时重定向我的应用程序,但它不工作。我只是想在我的 ngOnInit() 中使用 router.navigate,它返回 false。

我尝试将它插入到 setTimeout 中,然后它就可以工作了。

如果您有任何想法,将不胜感激:)

ngOnInit() {

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

}

【问题讨论】:

  • 添加代码以获取更多信息。
  • 完成了 micronyks :)
  • 好的,我正在创建一个新项目,以仅使用那段代码进行复制。
  • 我在默认路由上使用了redirectTo /hors-connexion,而不是在启动时重定向!谢谢。

标签: angular angular2-routing ngoninit


【解决方案1】:

我会这样尝试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2017-04-14
    • 2018-06-12
    • 2018-12-12
    • 2016-05-25
    • 1970-01-01
    • 2016-09-12
    相关资源
    最近更新 更多