我有一个组件:
import { Component, OnInit } from '@angular/core';
import { PostService } from '../shared/services/post.service';
import { CreatePost } from '../shared/model/post.model';
import { Router } from '@angular/router';
@Component({
selector: 'app-post',
templateUrl: './post.component.html',
styleUrls: ['./post.component.css']
})
export class PostComponent implements OnInit {
confirmPost:CreatePost[] = [];
unconfirmPost:CreatePost[] = [];
searchPost = '';
constructor(
private postService: PostService,
private router: Router
) { }
ngOnInit() {
this.postService.getPosts()
.subscribe((post: CreatePost[])=>{
this.confirmPost = post.filter(p => p.confirm === true).reverse();
this.unconfirmPost = post.filter(p => p.confirm === false);
});
}
watchPost(post:CreatePost){
this.router.navigate(['/system/post', post.id],{queryParams: post});
}
confirmePost(post:CreatePost){
post.confirm = true;
this.postService.updatePost(post)
.subscribe((poste:CreatePost[]) => {
this.confirmPost = this.confirmPost.unshift(poste);
});
}
}
CreatePost 模型:
export class CreatePost{
constructor(
public data: any,
public category: string,
public user: string,
public title: string,
public text: string,
public like: number,
public watch: number,
public confirm: boolean,
public id?: number
){}
}
当我尝试更新数组 this.confirmPost = this.confirmPost.unshift(poste); 我收到一个错误:
ERROR in src/app/system/post/post.component.ts(39,51): error TS2345: Argument of type 'CreatePost[]' is not assignable to parameter of type 'CreatePost'.
Type 'CreatePost[]' is missing the following properties from type 'CreatePost': data, category, user, title, and 4 more.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
你觉得他是什么类型的
CreatePost[]
?