【发布时间】:2019-05-23 07:18:01
【问题描述】:
我正在尝试获取单个帖子的详细信息,但似乎它返回的是所有帖子的数组,而不是点击一个帖子,
代码
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' },
有什么想法吗?
更新
正如你看到我的后端工作正常,问题出在我的前端功能(上面共享)
【问题讨论】:
-
你已经发布了你的前端代码,但是你的后端呢?这些问题是否可能与后端有关?你确定你调用的是正确的网址吗?如果对后端一无所知,将很难为您提供帮助。
-
也许我没看到,但是您将 id 添加到每个 url 以将 GET-Method 请求引导到正确的 URL 的行在哪里?
-
@liqSTAR 我正在使用
url (slug)而不是id
标签: javascript angular ionic-framework