繁体   English   中英

Angular:标识符“标题”未定义。 'never' 不包含这样的成员。 异常问题

[英]Angular: Identifier 'title' is not defined. 'never' does not contain such a member. Unusual problem

我正在学习教程,但我被卡住了。 不知道为什么我会收到错误消息。 visual studio 代码上的错误是“标识符‘title’未定义。‘never’不包含这样的成员”对于 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>

在 app.component.ts 中,我收到一个错误:“参数‘post’隐式具有‘any’类型”和“‘any’类型的参数不可分配给‘never’类型的参数”

这个特定的错误是在 onPostadded

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);
  }
}

让我知道是否需要显示更多不同文件的代码。 如果能得到任何帮助,我将不胜感激。

编辑:对于 function "*ngFor="let post of posts",出于某种原因,Posts 类型为 never: (property) PostListComponent.posts: never[]

我不确定为什么会这样。

我应该补充一点,posts 变量是一个 const 数组。

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" 因为你在 storedPost 提交时添加你的帖子

//This is how you add interface
interface IPost {
 title: string;
 content: string;
}

export class PostListComponent {
  @Input() posts: IPost[] = [];
}

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

了这个你就会明白

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM