Angular 2 Resolve - 局部变量范围

时间:2017-06-05 21:03:01

标签: javascript angular typescript

我是棱角分明的新手,我一直在玩弄决心。但是,当我想传递一个本地布尔值时,我得到了未定义的?我想知道为什么。以下是我的警卫,我的服务只是调用api而且它正在通过。我想将布尔值传递给附加解析的组件,以便显示错误。

resolve(): boolean {
    let passed: boolean;
    let uid: string = window['appdata'].uid;
    let tbt: string = window['appdata'].tbt
    let lang: string = window['appdata'].apiUserLanguage;

    if(uid != null || undefined && tbt != null && undefined){
        console.log('Uid is: ' + uid + ' tbt is: ' + tbt);
        this.validateEmailService.emailCheck(uid, tbt, lang).subscribe( 
            data => {
                passed = true;
            },
            err => {
                passed = false;
            }
        );
    }
    console.log('Passed is: ' + passed);
    return passed;
}

}

1 个答案:

答案 0 :(得分:2)

将返回布尔值,但您可以在订阅服务返回的observable后显示它,因为subscribe是一个异步方法。尝试将其记录到subscribe这样的&它的值将显示:)

resolve(): boolean {
let passed: boolean;
let uid: string = window['appdata'].uid;
let tbt: string = window['appdata'].tbt
let lang: string = window['appdata'].apiUserLanguage;

if(uid != null || undefined && tbt != null && undefined){
    console.log('Uid is: ' + uid + ' tbt is: ' + tbt);
    this.validateEmailService.emailCheck(uid, tbt, lang).subscribe( 
        data => {
            passed = true;
            console.log('Passed is: ' + passed);// Passed is: true
        },
        err => {
            passed = false;
            console.log('Passed is: ' + passed);// Passed is: false
        }
    );
}

return passed;

}