0
I’m creating a little evaluation project, and I ran into a problem. I created a function in a checkin model that exists in my application, however, I cannot call it anywhere...
Note. Instead of the fixed value I will have a logic, I need to know what is wrong before creating such logic.
Model js.
import { CheckIn } from './models/checkIn';
export class Consulta {
checkIn: CheckIn[];
valorGasto: number = 0;
valorTotal(){
this.valorGasto = 666;
}
}
The Total Value function is where it impales you.
import { Component, Input, OnInit } from '@angular/core';
import { CheckIn } from '../models/checkIn';
import { Consulta } from '../models/Consulta';
@Component({
selector: 'app-consultas',
templateUrl: './consultas.component.html',
styleUrls: ['./consultas.component.sass']
})
export class ConsultasComponent implements OnInit{
filtro: any;
@Input() checkIns: CheckIn[] = [];
consulta: Consulta[];
consultas: Consulta[];
ngOnInit(){
this.consulta = [];
this.consultas = [];
}
ngAfterContentChecked(){
console.log("Vzio");
if(this.checkIns.length != 0 && this.consultas.length == 0){
console.log("cheio");
this.checkIns.map(value => {
this.consulta.valorGasto = 333;
this.consulta.checkIn = value;
Aqui dá problema => this.consulta.valorGasto = this.consulta.valorTotal();
this.consultas.push(this.consulta);
})
}
}
get getCheckIns(): Consulta[] {
if(this.filtro == 1){
return this.consultas.filter(option => new Date(option.checkIn.dataSaida) > new Date());
}else if(this.filtro == 2){
return this.consultas.filter(option => new Date(option.checkIn.dataSaida) < new Date());
}else{
return this.consultas;
}
}
}
Appcomponent.html:8 ERROR Typeerror: this.consulta.valorTotal is not a Function
at queries.component.ts:30
at Array.map ()
At Consultscomponent.ngAfterContentChecked (queries.component.ts:27)
at callProviderLifecycles (core.js:3232324)
at callElementProvidersLifecycles (core.js:32293)
at callLifecycleHooksChildrenFirst (core.js:32275)
checkat AndUpdateView (core.js:44276)
at callViewAction (core.js:44637)
at execComponentViewsAction (core.js:44565)
checkat AndUpdateView (core.js:44278)
The problem is that the component is not receiving an array of the correct type. As is the Ué component has the element
<app-consultas>
?– Costamilam
I don’t understand... If there is the Query object in the.ts component I can’t call the functions that come attached to it? Since they were created inside the object...
– vitor paulo Galvan
At compile time it may be of the correct type but in execution there is no type (there is but it is not strong), so there is no guarantee that such an object is in fact of a certain type (unless you keep doing
instance of
all the time). If you receive this array from an API in JSON format, the incoming object has no methods and the TS does not make a cast automatic– Costamilam