无法通过JSON

时间:2019-07-10 10:24:17

标签: ionic-framework ionic4

正在寻找一种获取帖子特色图片的方法,但尽管使用了_embed,但似乎并未使用{{post._embedded ['wp:featuredmedia']。link}}输出数据。想知道我的代码是否有什么问题,有人可以指导我吗?

export class WordPressRestapiService {

  baseRestApiUrl: string = this.appConfig.Shop_URL + '/wp-json/wp/v2/';

  constructor(private httpClient: HttpClient, public appConfig: AppConfig) { }

  getRecentPosts(categoryId: number, page: number = 1): Observable<Post[]> {
    // Get posts by a category if a category id is passed
    let category_url = categoryId ? ("&categories=" + categoryId) : "";

    return this.httpClient.get(this.baseRestApiUrl + "posts?_embed&page=" + page + category_url ).pipe(
      map((posts: Post[]) => {
        return posts.map((post) => new Post(post));
      }),
      catchError(error => {
        return Observable.throw('Something went wrong ;)');
      })
    );
  }

  getPost(postId: number): Observable<Post> {
   return this.httpClient.get(this.baseRestApiUrl + "posts/" + postId + "?_embed").pipe(
     map(post => {
       return new Post(post);
     }),
     catchError(error => {
       return Observable.throw('Something went wrong ;)');
     })
   );
 }

}

1 个答案:

答案 0 :(得分:0)

在.html文件中使用

来获取帖子特征图片(如果您的json名为“ post”)
<img  src="{{post?._embedded['wp:featuredmedia'][0]?.media_details.sizes.full.source_url}}">