我正在尝试获取单个帖子的详细信息,但它似乎返回所有帖子的数组,而不是单击一个帖子,
Post service
export class PostsService {
apiUrl = 'https://example.com/api/posts/';
constructor(private http: HttpClient) { }
getPosts(): Observable<any> {
return this.http.get(`${this.apiUrl}`).pipe(
map(posts => posts)
);
}
//getting single post
getDetails(url) {
return this.http.get(`${this.apiUrl}?i=${url}&plot=full`);
}
}
post detail controller
export class PostsDetailsPage implements OnInit {
post: any;
constructor(private activatedRoute: ActivatedRoute, private postsService: PostsService) { }
ngOnInit() {
let url = this.activatedRoute.snapshot.paramMap.get('url');
this.postsService.getDetails(url).subscribe(res => {
this.post = res;
console.log(res); //see next part
});
}
}
console.log(res); returns
(73) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
res
在我的帖子控制器中应该返回单个帖子详细信息,但它的行为与我的博客页面相同(返回所有帖子)。
routes
{ path: 'posts', loadChildren: './pages/posts/posts.module#PostsPageModule' },
{ path: 'posts/:url', loadChildren: './pages/posts-details/posts-details.module#PostsDetailsPageModule' },
任何想法?
如您所见,我的后端工作正常,问题出在我的前端功能上(上面已分享)
问题是由我的 get url 引起的,我将其更改为下面的代码,现在工作正常,
getDetails(url) {
return this.http.get(`${this.apiUrl}${url}`).pipe(
map(post => post)
);
}
我删除?i=
并&plot=full
从中获取部分。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句