错误 TS2339:属性 'addForm' 在类型 'PostsComponent' 上不存在

error TS2339: Property 'addForm' does not exist on type 'PostsComponent'

这是错误信息:

ERROR in src/app/components/posts/posts.component.html:6:58 - error TS2339: Property 'addForm' does not exist on type 'PostsComponent'.
<form #postForm="ngForm" (ngSubmit)="addForm(postForm)"> 

src/app/components/posts/posts.component.ts:7:16
templateUrl: './posts.component.html',
Error occurs in the template of component PostsComponent.

值得注意的是,只有当我在 posts.component.html 文件中写入以下行时才会发生错误:

<form #postForm="ngForm" (ngSubmit)="addForm(postForm)">

这是我在 post/component.ts:

import { Component, OnInit } from '@angular/core';

import { PostService } from '../../services/post.service';

@Component({
  selector: 'app-posts',
  templateUrl: './posts.component.html',
  styleUrls: ['./posts.component.css'],  
  providers: [PostService],
}) 
export class PostsComponent implements OnInit {

  constructor(private postService: PostService) { }

  ngOnInit(): void {
  }

}

但我想错误实际上是在 post.service.ts 中,其中出现以下行:

import { PostsComponent} from '../components/posts/posts.component'

在我与 (https://www.youtube.com/watch?v=ccBtSAMFjto) 一起学习的介绍性视频中,最短 29:45 上面的行出现在第 4 行 以一种无法解释的方式 .实际上,最后一次显示该屏幕区域是在第 24 分钟,并且该行不存在。

当我在我的代码中写下这一行时,它看起来像阴影一样,好像它缺少一种关系,而在视频代码中却没有发生同样的事情。我想这与我的错误有关,因为它与 "PostsComponent".

有关

在视频的第 17 分钟,他遇到了同样的问题,似乎通过在 ** app.module.ts **:

中添加以下行来解决
import { FormsModule } from '@angular/forms';

我发现以前问过类似的问题,但无法找出发现我错误的常见原因。

在组件中创建 addForm 方法解决您的问题

import { Component, OnInit } from '@angular/core';
import { PostService } from '../../services/post.service';

@Component({
  selector: 'app-posts',
  templateUrl: './posts.component.html',
  styleUrls: ['./posts.component.css'],  
  providers: [PostService],
}) 
export class PostsComponent implements OnInit {

  constructor(private postService: PostService) { }

  ngOnInit(): void {
  }

  addForm(values){
  }

}