angular 2:订阅可观察收益率"undefined"?
angular 2: subscribing to observable yield "undefined"?
我有一个简单的服务,可以获取 github 用户的回购协议,并正确地 returns 一个 数组对象 。
github服务
getRepos(){
return this._http.get(this._url + this.username +"/repos")
.map(users =>{
this.u = users.json();
console.log("getRepos", this.u); //<== correctly returns array object
});
}
当我在组件中订阅它时,我得到未定义。
github分量
getReposFromGithub(){
return this._githubService.getRepos().subscribe(repos => {
this.repos = repos;
console.log("repos", repos);}, //<== logs 'undefined'
(error)=> console.log("error message",error), //<== no error message
()=>{console.log("completed");} //<== gets logged
);
}
ngOnInit() {
console.log("on changes");
this.getReposFromGithub();
}
仅供参考,这里是返回的数组对象
我在这里缺少什么?也许可以使用一些 rxjs 运算符?
提前致谢!!
您在 getRepos
中与 map
一起使用的 lambda 函数没有 return 任何东西。
您没有从 getRepos()
中的 .map
调用中 return 调用任何内容。您可能是想 return 结果数组。添加 return this.u;
,像这样:
getRepos() {
return this._http.get(this._url + this.username +"/repos")
.map(users => {
this.u = users.json();
console.log("getRepos", this.u); //<== correctly returns array object
return this.u;
});
}
我有一个简单的服务,可以获取 github 用户的回购协议,并正确地 returns 一个 数组对象 。
github服务
getRepos(){
return this._http.get(this._url + this.username +"/repos")
.map(users =>{
this.u = users.json();
console.log("getRepos", this.u); //<== correctly returns array object
});
}
当我在组件中订阅它时,我得到未定义。
github分量
getReposFromGithub(){
return this._githubService.getRepos().subscribe(repos => {
this.repos = repos;
console.log("repos", repos);}, //<== logs 'undefined'
(error)=> console.log("error message",error), //<== no error message
()=>{console.log("completed");} //<== gets logged
);
}
ngOnInit() {
console.log("on changes");
this.getReposFromGithub();
}
仅供参考,这里是返回的数组对象
我在这里缺少什么?也许可以使用一些 rxjs 运算符? 提前致谢!!
您在 getRepos
中与 map
一起使用的 lambda 函数没有 return 任何东西。
您没有从 getRepos()
中的 .map
调用中 return 调用任何内容。您可能是想 return 结果数组。添加 return this.u;
,像这样:
getRepos() {
return this._http.get(this._url + this.username +"/repos")
.map(users => {
this.u = users.json();
console.log("getRepos", this.u); //<== correctly returns array object
return this.u;
});
}