【发布时间】:2021-04-13 10:39:04
【问题描述】:
我是 Angular 的新手,还在学习它。
我有一个订阅路由器 NavigationEnd 事件的组件,该事件访问一些在服务解析的路由中初始化的变量。
constructor(private route: ActivatedRoute, private router: Router) {
var that = this;
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.nominatim = this.route.snapshot.data.meetings.nominatim;
console.log(this.nominatim);
}
});}
当我调试代码时,this.nominatim 变量是未定义的,但它确实包含一个我可以登录到控制台的值。
我在 stackoverflow 上发现了另一个问题,它解释了类似的情况 'This' is undefined inside subscribe
通过添加
var that = this;
在路由器事件订阅之前,我可以在调试中看到变量。
如果我仍然可以使用“this”上下文,我会更喜欢它。
我怎样才能让它工作?
【问题讨论】:
标签: angular typescript debugging angular-router