angular - Angular 类型不存在属性
问题描述
我有一个关于 Post[] 类型上不存在的属性的错误。所有代码都运行良好,但我的控制台中有此错误。
服务.ts
private objectAllPosts: BehaviorSubject<Post[]>;
public allPosts: Observable<Post[]>;
private listPosts: Post[] = [];
public tempPosts: Post[] = [];
constructor(private _http: HttpClient, private _toast: ToastService) {
this.objectAllPosts = new BehaviorSubject(null) as BehaviorSubject<Post[]>;
this.allPosts = this.objectAllPosts.asObservable();
}
public getAllPosts() {
this._http.get<Post[]>(environment.apiUrl + "/api/posts/").subscribe(
(res) => {
this.listPosts = res.posts; // MESSAGE ERROR
this.tempPosts = [...res.posts]; // MESSAGE ERROR
this.objectAllPosts.next(this.listPosts); // MESSAGE ERROR
},
(error) => {
console.log(error);
}
);
}
界面
export class Post {
public _id?: string;
public post: string;
public posts: [];
public comments?: object;
public createdAt?: object;
}
解决方案
这是一个 TS Lint 错误。解决它的正确方法是定义相应的类型。另一种避免它的方法是使用括号表示法而不是点表示法。在此处查找有关属性访问器的更多详细信息。
尝试以下
this.listPosts = res['posts'];
this.tempPosts = [...res['posts']];
推荐阅读
- ns2 - Ns2 802.11p 传播模型仿真参数
- google-sheets - 问:Google 表格 - 过滤相隔不到 24 个月的条目
- java - 使用 Firebase-UI (Android) 的 Firestore 聊天应用
- arrays - 在WebGL中将大数组传递给统一
- python - 在类的构造函数中获取变量并修改python
- c# - 带有视频文件的 .NET Nancy 响应(自托管)
- r - corrgram 包依赖
- firebase - this.$firebase 未定义
- arrays - split string (based on comma) into array adds an empty item at the end of list
- sql - 前 5 周的 T-SQL 返回金额?