javascript - 获取角度和离子的单个帖子细节
问题描述
我正在尝试获取单个帖子的详细信息,但似乎它返回的是所有帖子的数组,而不是点击一个帖子,
代码
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' },
任何的想法?
更新
如您所见,我的后端工作正常,问题出在我的前端功能(上面共享)
解决方案
解决了
问题是由我的获取网址引起的,我将其更改为下面的代码,现在工作正常,
getDetails(url) {
return this.http.get(`${this.apiUrl}${url}`).pipe(
map(post => post)
);
}
我删除?i=
并&plot=full
从获取部分。
推荐阅读
- python - 如何使用 sqlalchemy 从多个数据库中获取数据?
- image - Flutter image_picker不保存原点名称
- swift - 如何在字符串文字中表示 utf8 十六进制字符?
- c# - 无法在组合框中选择项目
- javascript - Puppeteer 如何根据元素数组从地图中记录对象列表,通过 $() 获取
- python - 更新谷歌表格后,气流随时触发 DAG
- constraints - Keycloak H2登录失败:约束已存在
- python - 深度神经网络不学习任何东西
- python - 使用带有代理(http)的sftp上传文件,使用python-paramiko
- bash - 使用 sed 更改复杂字符串 | grep