Error Initializing Model Class. Error (2693)

Asked

Viewed 23 times

0

I have the following error in my code:

**'AlunoModel' only refers to a type, but is being used as a value here.ts(2693)**

I need to create an object to put in my API, so I created a Model of my API object called Alunomodel and tried to start it as follows:

student: Alunomodel = new Alunomodel();

Need to fix this error to proceed with the application, this error is due to some tsconfig configuration or there is another way to do that code?

students.componentts.

import { Component, Oninit } from '@angular/core'; import { Alunosservice } from 'src/app/services/students.service'; import { Alunomodel } from '.. /.. /interfaces/student-model';

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

  aluno: AlunoModel = new AlunoModel();
  alunos: Array<any> = new Array();

  constructor(private alunosService: AlunosService) { 
    this.listarAlunos();
  }

  ngOnInit(): void {
  }

  cadastrar(){
    console.log(this.alunos);
  }

  listarAlunos(){
    this.alunosService.listarAlunos()
    .subscribe( alunos => {
      this.alunos = alunos;
    },
      () => {alert('Falha ao Listar Alunos!');})
  }
}

students.component.html

<h2>Alunos Cadastrados:</h2>

<table>
    <thead>
      <tr>
        <th scope="col">ID:</th>
        <th scope="col">NOME:</th>
        <th scope="col">IDADE:</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let aluno of alunos">
        <td>{{aluno.id}}</td>
        <td>{{aluno.nome}}</td>
        <td>{{aluno.idade}}</td>
      </tr>
    </tbody>
  </table>

  <h2>Cadastro de Alunos:</h2>

  <form>
      Nome: <input type="text" name="nome" [(ngModel)]="aluno.nome">     
      Idade: <input type="text" name="idade" [(ngModel)]="aluno.idade">
      <br>
      <br>
      <button type="button" (click)="cadastrar()">Cadastrar</button>
  </form>

1 answer

1


Well, after a lot of tests... What was happening was that in the Alunomodel model the export was being made as:

export interface AlunoModel

I changed the export and made it like:

export class AlunoModel

The error disappeared soon after the change...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.