angular - 错误 TS2339:“对象”类型上不存在属性“funcionarios”
问题描述
当项目开始运行时,我在 Angular CLI 中收到此错误:
src/app/app.component.ts(44,37) 中的错误:错误 TS2339:“对象”类型上不存在属性“funcionarios”。src/app/app.component.ts(51,36):错误 TS2339:“对象”类型上不存在属性“funcionario”。
我该如何解决这个问题?
这是 Angular app.component.ts 文件:
import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormGroup, FormControl, FormArray, NgForm } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
@Injectable()
export class AppComponent implements OnInit {
title = 'Angular + Symfony CRUD';
private funcionarioForm: FormGroup;
funcionario: any;
funcionarios: any;
constructor(private http: HttpClient) {
this.getFuncionarios();
}
ngOnInit() {
this.funcionarioForm = new FormGroup({
'nome': new FormControl(),
'matricula': new FormControl(),
'email': new FormControl()
});
}
// Adiciona um novo funcionario
adicionarFuncionario(funcionarioForm: NgForm) {
this.http.post('http://127.0.0.1:8000/funcionarios', funcionarioForm.value).subscribe(res => {
this.getFuncionarios();
funcionarioForm.reset();
}, err => {
console.log('Ocorreu um erro');
});
}
getFuncionarios() {
return this.http.get('http://127.0.0.1:8000/funcionarios').subscribe(res => {
this.funcionarios = res.funcionarios;
});
}
showFuncionario(id) {
return this.http.get('http://127.0.0.1:8000/funcionarios/' + id).subscribe(res => {
this.funcionario = res.funcionario;
this.funcionarioForm.patchValue({
id: this.funcionario.id,
nome: this.funcionario.nome,
matricula: this.funcionario.matricula,
email: this.funcionario.email
});
});
}
deletarFuncionario(id) {
this.http.delete('http://127.0.0.1:8000/funcionarios/' + id).subscribe(res => {
console.log('Product Deleted and refresh Table');
this.getFuncionarios();
}, err => {
console.log('Ocorreu um erro');
});
}
atualizarFuncionario(id) {
this.http.put('http://127.0.0.1:8000/funcionarios/' + id, this.funcionarioForm.value).subscribe(res => {
console.log('Product Updated and refresh table');
this.getFuncionarios();
}, err => {
console.log('Ocorreu um erro');
});
}
}
解决方案
推荐阅读
- javascript - 在 for 循环中 google-translate-api 获取随机索引值,为什么不遵循模式?
- python - Kivy:我希望窗口大小的按钮堆叠在屏幕顶部
- sum - prometheus sum after rate 功能背后的逻辑是什么?
- apache-kafka - Kafka Stream 只消费 N 条消息
- javascript - 使用 JS 使用正则表达式创建数字仍然创建重复数字
- r - 如何读取文件夹中的 .xlsm 文件,该文件夹使用 R 存在于不同文件夹中
- linux - Modbus RTU 到 Raspberry Pi/Linux 到 Modbus TCP
- javascript - 如何在 ReactJS 中定位 DOM 元素?
- python - 使用 Pandas 与 CSV 读取器/写入器处理和保存大型 CSV 文件
- excel - 在 Microsoft Excel 中单击按钮时如何在单元格中播放音频文件 (.Mp3)