简体   繁体   English

属性“ campanha”在类型“ DisputaComponent []”上不存在-Angular 2

[英]Property 'campanha' does not exist on type 'DisputaComponent[]' - Angular 2

I'm trying to access a property inside an object called disputas but I'm getting this message: 我正在尝试访问名为disputas的对象内的属性,但收到此消息:

[ts] Property 'campanha' does not exist on type 'DisputaComponent[]' [ts]属性'campanha'在类型'DisputaComponent []'上不存在

I can't access any property inside disputas , I think it's because its returning an Array of disputas , so how can I access each object disputa inside this array? 我无法访问disputas内的任何属性,我认为这是因为它返回一个disputas数组,所以如何访问该数组内的每个对象disputa

What I'm trying to do is show only objects with the same ID of the page, here's the snippet code: 我想做的是仅显示具有相同页面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"))
}

and here's the full code if you guys need it: 如果需要的话,这是完整的代码:

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));
      })
    }

Thanks in advance :) 提前致谢 :)

You are retrieving an array of disputas and trying to find the ones with the same cliente_id as the one in this.campanha . 您正在检索一系列disputas并尝试找到与cliente_id中的this.campanha相同的this.campanha The array itself does not have this property, you should filter the array, and then set the result: 数组本身不具有此属性,应过滤数组,然后设置结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM