这是错误消息:
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){
}
}