[英]Angular 6 HttpClient using Map
For learning purposes I am trying to get datas from a json fake API and add "hey" to all titles before returning an Observable. 出于学习目的,我试图从json假API中获取数据,并在返回Observable之前在所有标题中添加“嘿”。 So far I can display the data if I don't use Map and even while using map if I console.log my variable it says Observable but it does not display in my template.
到目前为止,如果我不使用Map,即使在使用map的情况下,如果我console.log我的变量它显示为Observable,但仍可以显示数据,但它不会显示在模板中。
<div class="col-6" *ngIf="courseDatas$ | async as courses else NoData">
<div class="card" *ngFor="let course of courses">
<div class="card-body">
<span><strong>User ID is : </strong>{{course.userId}}</span><br>
<span><strong>Title is : </strong>{{course.title}}</span><br>
<span><strong>Body is : </strong>{{course.body}}</span><br>
<span><strong>ID is : </strong>{{course.id}}</span>
</div>
<ng-template #NoData>No Data Available</ng-template>
</div>
</div>
App component : 应用程序组件:
import {Component, OnInit} from '@angular/core';
import {PostsService} from "./posts.service";
import {Observable} from "rxjs";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
courseDatas$ : Observable<any>;
constructor(private posts : PostsService){}
ngOnInit(){
this.courseDatas$ = this.posts.getData();
}
}
posts Service : 邮政服务:
import { Injectable } from '@angular/core'
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class PostsService {
private postURL: string = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) {}
getData(): Observable<any> {
return this.http.get(this.postURL).pipe(
map(data => {
for (let datas of (data as Array<any>)){
datas.title = datas.title + "Hey";
}
}),
);
}
So, if I don't use the map operator in my getData method in my service everything displays properly. 因此,如果我在服务的getData方法中未使用map运算符,则所有内容都会正确显示。 If I use the map operator if I console.log coursesDatas$ in App.Component the console says Observable so I don't understand why it does not work with my async pipe in the template.
如果我在App.Component中使用console.logcoursesDatas $时使用地图运算符,则控制台显示“可观察”,因此我不明白为什么它不适用于模板中的异步管道。 Also, if I use console.log(datas.title) inside my map operator it does log every titles with Hey at the end.
另外,如果我在地图运算符中使用console.log(datas.title),它的末尾确实会记录每个标题。
map
should return something to mutate current property, in your case I guess you should return the data
map
应该返回一些东西来改变当前属性,在你的情况下,我想你应该返回data
getData(): Observable<any> {
return this.http.get(this.postURL).pipe(
map(data => {
for (let datas of (data as Array<any>)){
datas.title = datas.title + "Hey";
}
return data;
}),
);
}
by the way you can use Array.prototype
's map
instead of for
loop too, to mutate your data 顺便说一下,您也可以使用
Array.prototype
的map
而不是for
循环来变异数据
getData(): Observable<any> {
return this.http.get(this.postURL).pipe(
map(data => data.map(d => (d.title = d.title +"Hey", d));
}),
);
}
note that if curly braces are missing in arrow function, it will return automatically 请注意,如果箭头功能中缺少花括号,它将自动返回
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.