简体   繁体   English

Angular App中的函数运行时传递的值不可用

[英]Passed-in Values Not Available at Run-time of Function in Angular App

I realize there is something I'm missing in terms of how and specifically when the products of certain functions are available in JavaScript. 我知道有我丢失的东西在如何以及特别是某些功能的产品都可以在JavaScript方面。

In my Angular app, in order to get a user's initials, I am parsing data being returned from the API, and retrieving the first letter of the firstName, as well as the first letter of lastName in two different functions. 在我的Angular应用程序中,为了获取用户的姓名首字母,我正在解析从API返回的数据,并在两个不同的函数中检索firstName的首字母和lastName的首字母。 These two functions are working as expected, and I can see the correct results in the console: 这两个功能按预期工作,并且我可以在控制台中看到正确的结果:

getFirstNameFirstLetter() {
    if (this.authenticationService.isAuthenticated()) {
        const userObj = JSON.parse(sessionStorage.getItem('currentUser'));
        const userInfo = userObj.data;
        const firstName = userInfo.name.first;
        const firstNameFirstLetter = firstName.trim().charAt(0);
        console.log(firstNameFirstLetter);
        return firstNameFirstLetter;
    }
}

getLastNameFirstLetter() {
    if (this.authenticationService.isAuthenticated()) {
        const userObj = JSON.parse(sessionStorage.getItem('currentUser'));
        const userInfo = userObj.data;
        const lastName = userInfo.name.last;
        const lastNameFirstLetter = lastName.trim().charAt(0);
        console.log(lastNameFirstLetter);
        return lastNameFirstLetter;
    }
}

Now comes the part I'm not fully understanding. 现在是我尚未完全理解的部分。 When I then pass the returned values of these two functions, in order to get the initials, like this: 然后,当我传递这两个函数的返回值时,为了获得首字母,如下所示:

getInitials(firstNameFirstLetter, lastNameFirstLetter) {
    if (this.authenticationService.isAuthenticated()) {
        if (!this.firstNameFirstLetter || !this.lastNameFirstLetter) {
            console.log('Names not ready!');
            return;
        } else if (this.firstNameFirstLetter && this.lastNameFirstLetter) {
            console.log(firstNameFirstLetter + lastNameFirstLetter);
            return firstNameFirstLetter + lastNameFirstLetter;
        }
    }
}

... I get "Names not ready!" ...我得到“名称未准备好!” printed to the console each time. 每次都打印到控制台。

By the way, I am running these functions within Angular's ngOnInit life cycle hook, like this: 顺便说一下,我正在Angular的ngOnInit生命周期挂钩中运行这些函数,如下所示:

ngOnInit() {
    this.getFirstNameFirstLetter();
    this.getLastNameFirstLetter();
    this.getInitials(this.firstNameFirstLetter, this.lastNameFirstLetter);
}

I know this has something to do with what's available when, because I get 'undefined' when I use break points and debug the two values being passed into the "getInitials()" function. 我知道这与什么时候可用有关,因为当我使用断点并调试传递到“ getInitials()”函数中的两个值时,我得到“未定义”。 In other words, the function doesn't have access to the returned values of the other two functions at the time it's run -- hence I'm getting 'Names not ready!' 换句话说,该函数在运行时无法访问其他两个函数的返回值-因此,我得到的是“名称未准备好!” printed to the console. 打印到控制台。 My question is, what am I missing, architecturally, to resolve this kind of issue? 我的问题是,从结构上讲,我在解决这种问题上缺少什么?

So what is happening here is that JavaScript doesn't think you are using the return values for getFirstNameFirstLetter and getLastNameFirstLetter, so when it makes the call, instead of waiting for that call to finish, it goes on to the next one, which introduces a race condition. 因此,这里发生的是JavaScript认为您没有使用getFirstNameFirstLetter和getLastNameFirstLetter的返回值,因此当它进行调用时,而不是等待该调用完成,它继续进行下一个调用,它引入了比赛条件。 if you simply change it to 如果您只是将其更改为

ngOnInit() {
    let temp1 = this.getFirstNameFirstLetter();
    let temp2 = this.getLastNameFirstLetter();
    this.getInitials(this.firstNameFirstLetter, this.lastNameFirstLetter);
}

then it will wait for the previous functions to finish before calling the next. 然后它将等待上一个函数完成,然后再调用下一个。


Also, I don't use const very often, so I could be wrong and it could follow different scope rules, but by normal scope rules, setting a variable in that function, it is only available in that function, you would need to set it as 另外,我不经常使用const,所以我可能会错,它可能遵循不同的作用域规则,但是按照正常的作用域规则,在该函数中设置变量,则该变量仅在该函数中可用,您需要设置它作为

this.firstNameFirstLetter = firstName.trim().charAt(0);

to have access to it outside the function. 在功能之外访问它。


Or, so as to kill two birds with one stone, you could do 或者,为了用一块石头杀死两只鸟,您可以

ngOnInit() {
    this.firstNameFirstLetter = this.getFirstNameFirstLetter();
    this.lastNameFirstLetter = this.getLastNameFirstLetter();
    this.getInitials(this.firstNameFirstLetter, this.lastNameFirstLetter);
}

or 要么

ngOnInit() {
    let firstNameFirstLetter = this.getFirstNameFirstLetter();
    let lastNameFirstLetter = this.getLastNameFirstLetter();
    this.getInitials(firstNameFirstLetter, lastNameFirstLetter);
}

depending on if you need the variables again or just for that function. 取决于您是再次需要变量还是仅需要该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM