在函数getPosts()
和getMedia()
的声明中出现错误
我正在遵循一个古老的指南,使用Ionic创建一个ibrid应用程序,该应用程序显示了我的Wordpress网站的帖子,可能存在一些兼容性问题。
这是我的服务wp-provider.service.ts
的代码(我正在尝试使用Wordpress REST API)
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs';
import { WpApiPosts, WpApiMedia, WpApiUsers } from 'wp-api-angular';
import {map} from 'rxjs/operators';
export class Post {
public media_url: Observable<string>;
constructor(public authorId: number, public id: number, public title: string, public content: string, public excerpt: string, public date: string, public mediaId?: number) { }
}
export class User {
constructor(public id: number, public name: string, public userImageUrl: string) { }
}
@Injectable({
providedIn: 'root'
})
export class WpProviderService {
users: User[];
constructor(public wpApiPosts: WpApiPosts, public wpApiMedia: WpApiMedia, public wpApiUsers: WpApiUsers) {
this.wpApiUsers.getList()
.map(res => res.json())
.subscribe(data => {
this.users = [];
for (let user of data) {
let oneUser = new User(user[ 'id' ], user[ 'name' ], user[ 'avatar_urls' ][ '96' ]);
this.users.push(oneUser);
}
})
}
getPosts(): Observable<Post[]> {
return this.wpApiPosts.getList()
.map(res => res.json())
.map(data => {
var posts = [];
for (let post of data) {
let onePost = new Post(post[ 'author' ], post[ 'id' ], post[ 'title' ][ 'rendered' ], post[ 'content' ][ 'rendered' ], post[ 'excerpt' ][ 'rendered' ], post[ 'date' ], post[ 'featured_media' ]);
onePost.media_url = this.getMedia(onePost.mediaId);
posts.push(onePost);
}
return posts;
});
}
getMedia(id: number): Observable<string> {
return this.wpApiMedia.get(id)
.map(res => res.json())
.map(data => {
return data[ 'source_url' ];
});
}
}
我得到的错误:
Type 'Observable<any[]>' is not assignable to type 'Observable<Post[]>'.
Types of property 'source' are incompatible.
Type 'import("c:/Users/Jack/Workspace/project-work/ionic-app/node_modules/wp-api-angular/node_modules/rxjs/Observable").Observable<any>' is not assignable to type 'import("c:/Users/Jack/Workspace/project-work/ionic-app/node_modules/rxjs/internal/Observable").Observable<any>'.
Property 'operator' is protected in type 'Observable<any>' but public in type 'Observable<any>'