首页 > 解决方案 > 获取角度和离子的单个帖子细节

问题描述

我正在尝试获取单个帖子的详细信息,但似乎它返回的是所有帖子的数组,而不是点击一个帖子,

代码

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' },

任何的想法?

更新

一

二

如您所见,我的后端工作正常,问题出在我的前端功能(上面共享)

标签: javascriptangularionic-framework

解决方案


解决了

问题是由我的获取网址引起的,我将其更改为下面的代码,现在工作正常,

getDetails(url) {
    return this.http.get(`${this.apiUrl}${url}`).pipe(
      map(post => post)
    );
}

我删除?i=&plot=full从获取部分。


推荐阅读