gpt4 book ai didi

javascript - 类型 'campanha' 上不存在属性 'DisputaComponent[]' - Angular 2

转载 作者:行者123 更新时间:2023-12-03 04:44:56 25 4
gpt4 key购买 nike

我正在尝试访问名为 disputas 的对象内的属性,但收到以下消息:

[ts] Property 'campanha' does not exist on type 'DisputaComponent[]'

我无法访问 disputas 内的任何属性,我认为这是因为它返回一个 disputas 数组,那么我如何访问每个对象 disputa 在这个数组中?

我想要做的是仅显示具有相同页面 ID 的对象,这是片段代码:

constructor(service: DisputaService, private route:ActivatedRoute, 
private router:Router, private campanha_service:FiltroNegociacaoService){
service
.lista()
.subscribe(disputas => {
if (this.disputas.campanha.cliente_id == this.campanha.cliente_id) // this is where I get the message
this.disputas = disputas;
console.log("Disputas: ", disputas);
console.log("Campanha: ", this.campanha);
}, erro => console.log("erro"))
}

如果你们需要的话,这里是完整的代码:

import { Component, OnInit } from '@angular/core';
import {DisputaService} from '../services/disputas.service';
import {FiltroNegociacaoComponent} from '../../../filtra-negociacao/components/filtra-negociacao.component';
import {FiltroNegociacaoService} from '../../../filtra-negociacao/services/filtro-negociacao.service';
import {ActivatedRoute, Routes, RouterModule, Router} from '@angular/router';

@Component({
moduleId: module.id,
selector: 'disputas',
templateUrl: `disputas.component.html`,
providers: [DisputaService, FiltroNegociacaoService]
})

export class DisputaComponent implements OnInit {
public disputas:DisputaComponent[] = [];
public loading = false;
campanhas: FiltroNegociacaoComponent;
campanha:any;
service: DisputaService;
name: string;
proposta_inicial:number;
propostas_realizadas:number = 0;
maximo_propostas:number;
status = {
status_nome: ""
}
id:number;

constructor(service: DisputaService, private route:ActivatedRoute,
private router:Router, private campanha_service:FiltroNegociacaoService){
service
.lista()
.subscribe(disputas => {
if (this.disputas.campanha.cliente_id == this.campanha.cliente_id)
this.disputas = disputas;
console.log("Disputas: ", disputas);
console.log("Campanha: ", this.campanha);
}, erro => console.log("erro"))
}

ngOnInit():void{
this.route.params.subscribe(params =>{
let id = params['id'];
this.campanha_service
.buscaPorId(id)
.subscribe(campanha => {
this.campanha = campanha;
},
erro => console.log(erro));
})
}

提前致谢:)

最佳答案

您正在检索一个 disputas 数组,并尝试查找与 this.campanha 中的 cliente_id 相同的数组。数组本身没有这个属性,你应该过滤数组,然后设置结果:

.subscribe((disputas: DisputaComponent[]) => {
this.disputas = disputas.filter(
disputa => disputa.campanha.client_id === this.campanha.cliente_id
);
}

关于javascript - 类型 'campanha' 上不存在属性 'DisputaComponent[]' - Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42905213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com