2017-12-03 34 views
0

我如何链Angular4观察到的功能,如果我想通过从一层到另一结果链接可观察到的。在不同层中Angular4

第一层是Get方法数据服务。
第二层是不同的服务,我打电话给数据服务中获得的方法,并订阅它得到的结果。

现在我想订阅第二层服务在我的组件,让我怎么通过第二层服务传递来自数据服务的价值组成部分?

的一点是,我不希望有来自数据服务组件直接代码。

enter image description here

我想订阅GetProfile的组成部分,但我怎么在这里正确值返回后认购后又名值是由有DataService的GetOne功能?

回答

0

你或许可以用做()。

public getProfile =() : Observable<any> { 
    this.dataService.setEndPoint('api/account/getprofile'); 
    return this.dataService.GetOne() 
     .map(response => response.json()) 
     .do(response => { 
      ...... 
     }, error => { 
      ...... 
     }); 
} 
+0

好了,但我怎么回,我仍然得到回报必须返回值的错误,我不知道在哪里把另一个回报?在。做什么做的(),所以功能是幸福?如果我把返回的响应,我不知道还有什么地方的返回值? –

0

哈,我最终通过了如何在一开始就完成它,不知道我在想什么,而不是在开始的事情。这是第二层,而我那么到底订阅:

public GetProfile =(): Observable<any> => { 
    this.dataService.SetEndpoint('api/account/getprofile'); 
    return this.dataService.GetOne(); 
} 

组件:

getProfile(): void{ 
    this.accountService 
     .GetProfile() 
     .subscribe(data => { 
      this._updateProfileModel = data; 

      this._form.setValue({ 
       firstName: this._updateProfileModel.firstName, 
       lastName: this._updateProfileModel.lastName, 
       phoneNumber: this._updateProfileModel.phoneNumber 
       }); 
     }, (error) => { 
      console.log(error); 
     }); 
}