Angular2 - 在后台路由页面

时间:2018-03-03 16:41:25

标签: javascript angular typescript angular2-routing

好的,这是问题所在。我的服务器'/ ping'上有一个端点,它检查它是否打开以及是否每个工作正常。我希望能够检测出现问题的时间,并在用户出现故障时向其显示页面。 这是我自己编写并且似乎正在工作的东西,但我想知道是否有更好的解决方案,因为我不希望在我的AppComponent中有这样的代码。

export class AppComponent {
    /* .. */
    constructor(){
        this.pingLoop()
    }

    isOffline = false;
    pingLoop(){
        this.apiService('/ping', () => {
            //All is good
            if(this.isOffline){
                this.router.navigateByUrl('home');                
            }

            this.isOffline = false;


            setTimeout(this.pingLoop, 1000);
        }, () => {
            //Error occured
            if(!this.isOffline){
                this.router.navigateByUrl('offline');
            }
            this.isOffline = true;

            setTimeout(this.pingLoop, 1000);
        })
    }
    /* .. */
}

0 个答案:

没有答案