Angular 2 http.put - 无法读取属性' atualizaStatus'未定义的

时间:2017-03-06 14:15:59

标签: rest angular angular2-services

我尝试使用put方法更新应用程序中的字段但是我在尝试这样做时遇到了一些问题。我是Angular 2的新手,但仍然不知道如何正确地做到这一点。

这是我的服务:

import {Injectable} from '@angular/core';
import {Http, Headers, Response, RequestOptions} from '@angular/http';
import {Component} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {DisputaPropostaComponent} from './disputas-proposta.component';
import 'rxjs/add/operator/map';

@Injectable()
export class DisputaPropostaService{

    contato:Object[] = [];
    name: string;
    headers:Headers;
    url: string = 'http://localhost:3004/disputas';

    constructor(private http: Http){}

    atualizaStatus (body:any): Observable<DisputaPropostaComponent[]>{
        let bodyString = JSON.stringify(body); // Stringify payload
        let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
        let options       = new RequestOptions({ headers: headers }); // Create a request option
        return this.http.put(`${this.url}/${body['id']}`, body, options)
                         .map((res:Response) => res.json()) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Ocorreu um erro em nosso servidor, tente novamente mais tarde')); //...errors if any
    }   
}

这是组件:

import { Component } from '@angular/core';
import {Http, Headers} from '@angular/http';
import {ActivatedRoute} from '@angular/router';
import {DisputaComponent} from '../../disputas/disputas.component';
import {DisputaService} from '../../disputas/disputas.service';
import {DisputaPropostaService} from './disputas-proposta.service';
import {disputaPropostas} from './proposta.interface';

@Component({
  moduleId: module.id,
  selector: 'detalhes',
  templateUrl: `disputas-proposta.component.html`,
  providers: [DisputaPropostaService]
})

export class DisputaPropostaComponent  {

    disputa: DisputaComponent;
    service: DisputaService;
    propostaService:DisputaPropostaService;
    route: ActivatedRoute;
    inputProposta = false;
    proposta:disputaPropostas = {proposta_usuario: null, proposta_cliente: null}

    constructor(service: DisputaService, route:ActivatedRoute, propostaService:DisputaPropostaService){
    // Some other code here...
    }

    recusaProposta(){
      if (this.disputa.propostas_realizadas == 0){
        this.propostaService.atualizaStatus(this.disputa)
          .subscribe(
            res => console.log(res),
            error=> console.log(error)
          );
      }
      this.inputProposta = true;
      this.disputa.propostas_realizadas++;
      if ((this.disputa.propostas_realizadas) == ((this.disputa.maximo_propostas)-1))
        alert("Ultima proposta")
    }

我收到的信息如下:

  

无法阅读财产“atualizaStatus&#39;未定义的

我错过了一些明显的东西吗?提前谢谢。

P.S。:如果某人有一篇解释Angular 2的REST的文章,我很高兴知道:)

2 个答案:

答案 0 :(得分:2)

这是实现依赖注入的正确方法

删除

service: DisputaService;
propostaService:DisputaPropostaService;
route: ActivatedRoute;

更新

constructor(private service: DisputaService,private route:ActivatedRoute, private propostaService:DisputaPropostaService){
    // Some other code here...
}

与[{3}}回答

的结果相同

我发现这本书有很多帮助。 Bougarfaoui El houcine

答案 1 :(得分:1)

问题出在组件构造函数

 constructor(service: DisputaService, route:ActivatedRoute, propostaService: DisputaPropostaService){
       this.service = service;
       this.route = route;
       this.propostaService = propostaService;
       // ...
    }