我想以100为例,但返回5000行。以及如何搭配使用地图。
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class PathsService {
path = 'https://jsonplaceholder.typicode.com/';
pathUser = this.path + 'users';
pathPost = this.path + 'posts';
pathContent = this.path + 'photos';
pathPostUserId = this.pathPost + '/?userId=';
constructor() { }
}
import { Injectable } from '@angular/core';
import { GetPosts } from '../entities/getPosts';
import { GetUsers } from '../entities/getUsers';
import { HttpClient } from '@angular/common/http';
import { GetDeneme } from '../entities/getdeneme';
import { PathsService } from './paths.service';
import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class GetdataService {
constructor(private http: HttpClient, private constsPath: PathsService) {
console.log(this.constsPath.pathPost);
console.log(this.constsPath.pathContent);
console.log(this.constsPath.pathUser);
console.log(this.constsPath.pathPostUserId);
}
getPosts(userid): Observable<GetPosts[]> {
if (userid) {
const link: string = this.constsPath.pathPostUserId + userid;
debugger;
return this.http.get<GetPosts[]>(link);
} else {
return this.http.get<GetPosts[]>(this.constsPath.pathPost);
}
}
getUsers(): Observable<GetUsers[]> {
return this.http.get<GetUsers[]>(this.constsPath.pathUser);
}
getContents(): Observable<GetDeneme[]> {
return this.http.get<GetDeneme[]>(this.constsPath.pathContent);
}
getProducts(seoUrl: string): Observable<GetDeneme[]> {
if (seoUrl) {
return this.http.get<GetDeneme[]>(this.constsPath.path + '/photos/' + seoUrl);
} else {
return this.http.get<GetDeneme[]>(this.constsPath.path + '/photos');
}
}
}
I want to take some for example 100 but return 5000 rows. and how can use map with
getPosts(): any {
debugger;
this.postService.getContents().pipe(take(100)).subscribe(res => {
this.getden = res;
});
};
我添加了我的代码博客服务链接; https://jsonplaceholder.typicode.com/photos; 我看着https://www.learnrxjs.io/operators/filtering/take.html这个页面无法正常工作或无法执行代码。为什么不起作用?
答案 0 :(得分:0)
使用map
运算符和slice
一起获取N个没有的行
getUsers(): Observable<GetUsers[]> {
return this.http.get<GetUsers[]>(this.constsPath.pathUser)
.pipe(
map(x => x.slice(0, 100)))
}