Angular 2,如何使用setTimeout?

Yif*_*fan 10 angular

在登录页面中,我在提交页面时有这个功能:

checkLogin(){
    this.x_userS.getLogin(this.x_userO.login_name, this.x_userO.pwd_plain).then(response => this.x_userO=response);
    (function(){
        setTimeout(() => {
            if (this.x_userO.login_status == "1") {
                this.x_companyS.getCompanyByUser(this.x_userO.user_id).then(response => this.x_companyO=response);
                (function(){setTimeout(() => {
                    this.x_userS.setUser(this.x_userO);
                    this.x_companyS.setCompany(this.x_companyO);
                    this.router.navigate(['HomePage']);
                }, 2000);
            })();
            }
            else {
                window.alert("oops");
            }
        }, 2000);
    })();
}
Run Code Online (Sandbox Code Playgroud)

其中x_userS是登录服务,x_userO是用户对象.我试图在处理它之前将promises提交两秒钟来返回数据.没有setTimeout,它不会及时返回它.

我尝试删除除警报以外的所有内容,并在两秒钟后验证它发生了.但是,它无法识别function(){}中的任何其他内容,所以我相信我需要传递我的所有服务和对象.

有谁知道如何做到这一点?

Gün*_*uer 21

如果使用function(),则this.不会指向类中的变量.() =>在任何地方使用.

无论如何,(function(){ ... })()周围setTimeout()似乎是多余的.