2016-06-17 8 views
0

Dies ist mein Code und ich konnte keine Lösung für mein Problem finden. Es tut mir leid, wenn die Antwort irgendwo existiert und nicht gefunden werden konnte.angular 2 rc - Eigenschaft 'Anfrage' von undefined nicht lesen

Dies ist die app.component.ts

import {Component} from '@angular/core'; 
 
import { HTTP_PROVIDERS} from '@angular/http'; 
 
import {SimpleHTTPComponent} from './simplehttp' 
 

 

 
@Component({ 
 
    selector: 'my-app', 
 
    providers: [HTTP_PROVIDERS], 
 
    template: ` 
 
    <div> 
 
     <simple-http></simple-http> 
 
    </div> 
 
    `, 
 
    directives: [SimpleHTTPComponent] 
 
}) 
 
export class AppComponent { 
 
\t 
 
\t data: Object; 
 
\t loading: boolean; 
 
\t constructor() { 
 
    \t } 
 
}

Das ist mein simplehttp.ts Datei

import {Component} from '@angular/core'; 
 
import {Http, Response} from '@angular/http'; 
 
import { HTTP_PROVIDERS } from '@angular/http'; 
 
import {Inject} from '@angular/core' 
 

 

 
@Component({ 
 
selector: 'simple-http', 
 
template: ` 
 
\t <h2>Basic Request</h2> 
 
\t <button type="button" (click)="makeRequest()">Make Request</button> 
 
\t <div *ngIf="loading">loading...</div> 
 
\t <pre>{{data | json}}</pre>`, 
 
providers:[HTTP_PROVIDERS] 
 
}) 
 
export class SimpleHTTPComponent { 
 
\t data: Object; 
 
\t loading: boolean; 
 
\t http: Http; 
 
\t constructor(@Inject(Http) private _http: Http){ 
 
\t \t 
 
\t } 
 
\t makeRequest():void{ 
 
\t \t this.loading = true; 
 
\t \t this.http.request('http://jsonplaceholder.typicode.com/posts/1') 
 
\t \t .subscribe((res: Response) => { 
 
\t \t this.data = res.json(); 
 
\t \t this.loading = false; 
 
\t \t }); 
 
\t } 
 

 
}

Antwort

1
Datei

Drei ist keine Notwendigkeit, HTTP_PROVIDERS mehr als einmal zur Verfügung zu stellen

Ihr Code hat einen Fehler. Sie sind zu _http Injektion aber http

export class SimpleHTTPComponent { 
    data: Object; 
    loading: boolean; 
    http: Http; 

    // injects to local variable `_http` 
    constructor(@Inject(Http) private _http: Http){} 

    makeRequest():void{ 
     this.loading = true; 
     // uses local variable `http` (without `_`) 
     this.http.request('http://jsonplaceholder.typicode.com/posts/1') 
     .subscribe((res: Response) => { 
     this.data = res.json(); 
     this.loading = false; 
     }); 
    } 
} 

Es sollte wie

export class SimpleHTTPComponent { 
    data: Object; 
    loading: boolean; 

    constructor(@Inject(Http) private _http: Http){ 

    } 
    makeRequest():void{ 
     this.loading = true; 
     this._http.request('http://jsonplaceholder.typicode.com/posts/1') 
     .subscribe((res: Response) => { 
     this.data = res.json(); 
     this.loading = false; 
     }); 
    } 
} 

sein verwenden Wenn der Konstruktor Parameter den Zugriffsmodifikator private oder public dann hat dies auch eine Instanz Eigenschaft automatisch erklärt.

+1

Sie sind schnell, ich war im Begriff, die gleiche Antwort zu schreiben. + 1 von mir :) –

+0

Der frühe Vogel bekommt den Wurm ;-) –

+0

Danke, ich weiß, dass ich irgendwo dumme Fehler gemacht habe. – edaddou

Verwandte Themen