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

Rest Angular 2 http.put-无法读取属性';atualizaStatus';未定义的,rest,angular,angular2-services,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} fr

我正在尝试使用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
    }   
}
我得到的信息如下:

无法读取未定义的属性“atualizaStatus”

我错过了什么明显的东西吗?先谢谢你


注:如果有人有一篇文章解释Angular 2的其余部分,我很高兴知道:)

问题在于您的组件构造函数

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

问题出在组件构造函数中

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

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

除去

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

constructor(private service: DisputaService,private route:ActivatedRoute, private propostaService:DisputaPropostaService){
    // Some other code here...
}
这和答案的结果相同


我发现这本书很有帮助

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

除去

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

constructor(private service: DisputaService,private route:ActivatedRoute, private propostaService:DisputaPropostaService){
    // Some other code here...
}
这和答案的结果相同


我发现这本书很有帮助

//这里还有其他代码。。。这包括这个吗。PropostService=PropostService?不,我应该把它放在那里吗?你们能解释一下为什么吗?我不是angular 2专家,但你们使用的是依赖注入,你们正在失去依赖的范围,在这个例子中是PropostService。您可以将构造函数更改为私有PropostService:DisputPropostService,并删除PropostService:DisputPropostService;非常感谢你,它真的帮助了我//这里有一些其他的代码。。。这包括这个吗。PropostService=PropostService?不,我应该把它放在那里吗?你们能解释一下为什么吗?我不是angular 2专家,但你们使用的是依赖注入,你们正在失去依赖的范围,在这个例子中是PropostService。您可以将构造函数更改为私有PropostService:DisputPropostService,并删除PropostService:DisputPropostService;谢谢你,这真的帮了我大忙谢谢你史蒂文:)谢谢你史蒂文:)