Parse date with Angular 4.3 HttpClient

后端 未结 5 925
再見小時候
再見小時候 2020-12-14 01:20

I\'m currently switching to the new HttpClient of Angular 4.3. One advantage is that I can specify a type info on the GET method and that the returned JSON is parsed into th

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 01:28

    Similar to Jonas Stensved's answer, but using pipes:

    import { map } from "rxjs/operators";
    
    this.http.get(url)
      .pipe(
        map(response => {
          response.mydate = new Date(response.mydate);
          return response;
        })
    

    Note the different import syntax of the map operator.

    Pipes were introduced in RxJS 5.5. They facilitate import handling, code readability, and reduce bundle size. See Understanding Operator Imports.

提交回复
热议问题