【发布时间】:2016-09-17 10:04:25
【问题描述】:
我想通过company.service 订阅getCompanies() 上的company-list.component。但是我收到以下错误:
无法读取未定义的属性“订阅”
这是代码:
company.service.ts
getCompaniesOfUser() {
let r;
this.userService.getUser().subscribe(
res=> r = res,
err => console.log(err),
()=>(this.getCompaniesWithSpecificLink())
)
}
getCompaniesWithSpecificLink() {
if (isAdmin == false) {
url = '/user/companies';
} else {
url = '/companies';
}
return this.getCompanies(url);
}
getCompanies(url):Observable<Company[]> {
return this.http.get(this.host + url)
.map((res:Response) => res.json());
}
公司列表.component.ts
companies:Company[];
public getTheCompanies() {
this._companyService.getCompaniesOfUser()
.subscribe(companies => this.companies = companies); <-- the error occurred here
}
【问题讨论】:
-
您需要创建一个自定义的 Promise/observable 以从 getCompaniesOfUser 返回它,参考 this answer 会帮助您。