角2,如何使用的setTimeout?

问题描述 投票:10回答:1

在登录页面,我有这样的功能,当他们提交页面:

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);
    })();
}

其中x_userS是登录服务和x_userO是用户对象。我想给的承诺2秒处理之前返回数据。如果没有的setTimeout,它不及时归还。

我想除了警报消除一切验证两秒后发生的。但是,它不承认任何其他的东西里面函数(){}所以我认为我需要通过我的所有服务和对象英寸

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

angular
1个回答
21
投票

如果你使用function(),然后this.不会指向变量类。使用() =>而是无处不在。

各地(function(){ ... })()setTimeout()似乎是多余的反正。

© www.soinside.com 2019 - 2024. All rights reserved.