bounty将在5天后过期。回答此问题可获得+200声望奖励。Chris Bordeman希望引起更多人对此问题的关注:请帮助Nuri解决这个 typescript 问题。她对编程很陌生,母语不是英语。我希望她能帮助她,不要让她气馁!:)
我收到以下错误:
**Error1:** Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays. Did you mean to use the keyvalue pipe?
**Error2:** this.blogs.push is not a function
我的代码如下所示:
export class BlogComponent {
blogs: Array<blogType>;
constructor() {
this.blogs = new Array<blogType>();
}
ngOnInit() {
this.blogs = JSON.parse(localStorage.getItem("blogs")!);
}
addBlog(title: any, content: any) {
let blog = new blogType(title.value, content.value);
if (localStorage.getItem('blogs')) {
this.blogs = JSON.parse(localStorage.getItem('blogs')!);
}
this.blogs.push(blog); //error occurs because of that line. Runtime error
localStorage.setItem('blogs', JSON.stringify(this.blogs));
title.value = '';
content.value = '';
alert('Blog Added!');
}
我试图从“blog”数组中获取数据并将其推送到“blog”数组中,以便将其存储到本地存储中,但由于以下行,我收到了一个错误:this.blogs.push(blog);
1条答案
按热度按时间r6l8ljro1#
检查LocalStorage的内容是否为null,然后再对其进行解析并分配给此。blog: