首页 > 解决方案 > 错误 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');
    });

}
}

标签: angulartypescript

解决方案


推荐阅读