我有一个订阅语句,我正在尝试调试它,但是当我在 VS Code 中单步执行时,“this”在语句内始终未定义。在这种情况下,“this.dataLoaded”未定义。调试时如何让它不被定义?
this.router.events
.filter(event => (event instanceof NavigationEnd))
.subscribe((routeData: any) => {
if(!this.dataLoaded){
...
}
});
当您使用时.subscribe()
那么在角度上它可能不会得到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
...
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)