相关疑难解决方法(0)

是否有一个等效的异步管道,您可以在组件内使用?

是否存在与async我可以在这样的组件内部使用的管道等效的东西

   @Component({
      selector: 'my-component',
    })

    export class myComponent {

      myObservable$: Observable<string>;

      method() {
        doSomething(this.myObservable$);
        // here I would like to access directly the current string value of
        // myObservable$ without having to subscribe 
      }
    }
Run Code Online (Sandbox Code Playgroud)

reactive-programming rxjs angular

10
推荐指数
2
解决办法
4943
查看次数

Angular - 指定可观察的返回类型?

我在一个函数中有这部分:

this.myservice.getComments()
  .subscribe(data => {
    this.post.comments = data.body;
    this.post.comments_count = Number(data.headers.get('x-wp-total'));
    this.post.comments_pages = Number(data.headers.get('x-wp-totalpages'));
    this.content_ready = true;
  });
Run Code Online (Sandbox Code Playgroud)

getComments函数:

export class myservice extends DataService {
  protected url = 'my_url';

  constructor( protected http: HttpClient ) {
    super(http);
  }

  getComments(){
    return this.get(
      this.subUrl, true );
  }
}
Run Code Online (Sandbox Code Playgroud)

dataService 的相关部分:

export class DataService {
  protected url: string;

  constructor(protected http: HttpClient) {}

  get(subUrl:string = '', needObservable = false) {
    let options = {};
    if(needObservable) {
      options = {observe: 'response'};
    }
    return this.http.get(this.url …
Run Code Online (Sandbox Code Playgroud)

rxjs typescript angular

10
推荐指数
2
解决办法
9269
查看次数

将 Observable 映射到对象数组

我有rest api,它返回带有数据的json数组。在客户端,我使用此代码从请求创建对象。

public getNews(): Observable<News[]> {

return this.http.get(this.baseUrl + '/news')
  .pipe(
    catchError(this.handleError('getNews', []))
  )
  .map(res => {
    let response: any = res;
    return response.map((item) => new News(item));
  });
}
Run Code Online (Sandbox Code Playgroud)

订阅:

this.restProvider.getNews().subscribe((news: News[]) => {
  this.news = news;
});
Run Code Online (Sandbox Code Playgroud)

但是,当响应不是数组而只有单个元素时,我该如何解决问题?或者响应是错误的,例如来自 API 的意外数据?

目前在这些情况下,我的应用程序抛出此错误:

response.map 不是函数

typescript ionic-framework angular

4
推荐指数
1
解决办法
2万
查看次数