gpt4 book ai didi

angular - 'This' 在 subscribe 中未定义

转载 作者:行者123 更新时间:2023-12-02 12:04:00 25 4
gpt4 key购买 nike

我有一个订阅语句,我正在尝试调试它,但是当我在 VS Code 中单步执行时,“this”在语句内始终未定义。在这种情况下,“this.dataLoaded”未定义。如何在调试时使其不为未定义?

this.router.events
.filter(event => (event instanceof NavigationEnd))
.subscribe((routeData: any) => {
if(!this.dataLoaded){
...
}
});

最佳答案

当您使用.subscribe()时,在 Angular 中它可能不会得到this。就我而言,我使用了另一个复制品。它总是对我有用。它也可能适合你。

var that = this; // Hear I store the this ref into another that
this.router.events
.filter(event => (event instanceof NavigationEnd))
.subscribe((routeData: any) => {
if(!that.dataLoaded){ // Hear you can use that instead of this
...
}
});

关于angular - 'This' 在 subscribe 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52974811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com