Angular 错误TS2339:属性';addForm';不存在于类型';PostsComponent';

Angular 错误TS2339:属性';addForm';不存在于类型';PostsComponent';,angular,mean,Angular,Mean,这是错误消息: 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.

这是错误消息:

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.
但我认为错误实际上出现在post.service.ts中,其中显示以下行:

import { PostsComponent} from '../components/posts/posts.component'
在我正在学习()的介绍性视频中,在第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){
  }

}

在组件中创建
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){
  }

}

您必须定义一个方法才能使用它,在哪里定义了
addForm
方法?在服务中?在视频(min min 17:20)中,它似乎在app.module.ts“import{FormsModule}from'@angular/forms';”中定义了它。我在下面尝试了Edison的建议,但没有成功。您必须定义一个方法才能使用它,该
addForm
方法定义在哪里?在服务中?在视频(min min 17:20)中,它似乎在app.module.ts“import{FormsModule}from'@angular/forms';”中定义了它。我在下面尝试了Edison的建议,但无效。我添加了“addForm(){}”行,错误保持不变。请尝试将
addForm()
更改为
addForm(values)
,例如:我已更新了我的答案是的,似乎有什么东西移动了。但现在我收到了以下错误消息:“src/app/components/posts/posts.component.html:8:87-错误TS2341:属性'postService'是私有的,只能在类'PostsComponent'中访问。”我添加了“addForm(){}”行,错误保持不变。请尝试将
addForm()
更改为
addForm(values)
,我更新了我的答案是的,好像有什么东西动了。但现在我收到了以下错误消息:“src/app/components/posts/posts.component.html:8:87-错误TS2341:属性“postService”是私有的,只能在类“PostsComponent”中访问。”
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){
  }

}