角度:标识符'title'未定义。 'never'不包含此类成员。异常问题



我按照教程进行操作,但我卡住了。 不知道为什么我会收到错误。 Visual Studio 代码上的错误是"标识符'标题'未定义。"从不"不包含这样的成员"对于 post.title 和 post.content

<mat-accordion multi=True *ngIf="posts.length > 0">
<mat-expansion-panel *ngFor="let post of posts">
<mat-expansion-panel-header>
{{ post.title }}
</mat-expansion-panel-header>
<p>
{{ post.content }}
</p>
</mat-expansion-panel>
</mat-accordion>
<p class="info-text mat-body-1" *ngIf=" posts.length <= 0" > no posts added yet</p>

和在应用程序中。 我收到一个错误:"参数'post'隐式具有'any'类型"和"类型'any'的参数不能分配给类型'从不'的参数">

此特定错误位于 onPostadd

import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'ang-project';
storedPost = [] ;
onPostAdded(post) {
this.storedPost.push(post);
}
}

如果我需要显示更多不同文件的代码,请告诉我。 我将不胜感激我能得到的任何帮助。

编辑: 对于函数 "*ngFor="let post of posts", 由于某种原因,帖子的类型从不 : (属性) PostListComponent.posts: never[]

我不知道为什么会这样。

我应该补充一点,帖子变量是一个常量数组。

import {Component, EventEmitter} from '@angular/core'
@Component({
selector: 'app-post-create',
templateUrl: './post-create.component.html',
styleUrls: ['./post-create.component.css']
})
export class PostCreateComponent {
enteredContent = '';
enteredTitle = '';
postCreated = new EventEmitter();
onAddPost() {
const posts = {
title: this.enteredTitle,
content: this.enteredContent
};
this.postCreated.emit(posts);
}
}

这是post-list-component.ts

import { Component, Input } from "@angular/core";
@Component({
selector: 'app-post-list',
templateUrl: './post-list.component.html',
styleUrls: ['./post-list.component.css']
})

export class PostListComponent {
// posts = [
//   {title:'first post', content:'this is the first post'},
//   {title:'second post', content:'this is the second post'},
//   {title:'third post', content:'this is the third post'}
// ];
@Input() posts: IPost[] = [];

我收到错误:"找不到名称'IPost'">

我不知道这是否:

<mat-accordion multi=True *ngIf="posts.length > 0">
<mat-expansion-panel *ngFor="let post of posts">
<mat-expansion-panel-header>
{{ post.title }}
</mat-expansion-panel-header>
<p>
{{ post.content }}
</p>
</mat-expansion-panel>
</mat-accordion>
<p class="info-text mat-body-1" *ngIf=" posts.length <= 0" > no posts added yet</p>

AppComponent的模板。但如果是,所以不要检查*ngIf="posts.length > 0"应该*ngIf="storedPost.length > 0"*ngIf="posts.length <= 0"应该*ngIf="storedPost.length <= 0"*ngFor="let post of posts"应该*ngFor="let post of storedPost"。因为您将提交时的帖子添加到存储的帖子中

//This is how you add interface
interface IPost {
title: string;
content: string;
}
export class PostListComponent {
@Input() posts: IPost[] = [];
}

Angular 现在默认生成一个具有严格模式的项目,您必须为变量指定类型,也许教程很旧,或者他们禁用了严格模式。

阅读此内容,您将了解

最新更新