【发布时间】:2021-05-23 22:39:35
【问题描述】:
这是我的代码:
ngOnInit() {
this.route.paramMap.subscribe(params => {
console.log(params.get('id')); // THIS IS WORKS
this.currentOrganization = this.getOrganizationId(params.get('id'));
console.log(this.currentOrganization); // THIS IS UNDEFINED
} );
}
getOrganizationId(id: string) {
let organizations = this.orgService.getOrganizations(); // THIS IS WORKS
return organizations.find(p => p.id == id);
}
为什么未定义 this.currentOrganization?如何获取 params.get('id') 的组织信息?
【问题讨论】:
-
return organizations.find(blah)是真的返回 id 还是未定义? -
也许你可以先 console.log
organizations.find(p => p.id == id)看看它是否真的返回任何值。 -
你有异步问题。你需要订阅它并把它带入订阅
标签: angular typescript ionic-framework routes