我想知道你是否能帮忙。我遵循这门课,https://www.udemy.com/the-complete-angular-master-class/learn/lecture/7441148#questions
我有个错误:
src/app/post/posts.Component.ts(21,11)中的错误: ERROR TS2696:“Object”类型可分配给极少数其他类型。你是想用“任何”字代替吗?类型‘any[]’中缺少以下属性: length、pop、push、concat和其他26个属性。src/app/post/posts.Component.ts(32,33):错误TS2339:属性'id‘在类型'Object’上不存在。
import { BadInput } from './../common/bad-input';
import { NotFoundError } from './../common/not-found-error';
import { AppError } from './../common/app-error';
import { PostService } from './../services/post.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.css']
})
export class PostsComponent implements OnInit {
posts: any[];
constructor(private service: PostService) {}
ngOnInit() {
this.service.getPosts()
.subscribe(
response => {
this.posts = response
});
}
createPost(input: HTMLInputElement) {
let post : any = { id: null, title: input.value };
input.value = '';
this.service.createPost(post)
.subscribe(
response => {
post['id'] = response.id;
this.posts.splice(0, 0, post);
console.log(response);
},
(error: AppError) => {
if (error instanceof BadInput) {
//this.form.setErrors(error.originalError)
} else {
throw error;
}
});
}
updatePost(post) {
this.service.updatePost(post)
.subscribe(
response => {
console.log(response);
});
}
deletePost(post) {
this.service.deletePost(99999)
.subscribe(
response => {
let index = this.posts.indexOf(post);
this.posts.splice(index, 1);
},
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log(error);
} else {
throw error;
}
});
}
}
发布于 2019-05-24 04:47:20
您需要说响应类型为any,以消除该错误,
this.service.createPost(post)
.subscribe(
(response :any) => {
发布于 2019-05-24 05:54:07
响应是一个数组,但没有一个名为id的值。或者它达不到'id',你可能有多个。
https://stackoverflow.com/questions/56292980
复制