2017-11-30 16 views
0
정의되지 않은

의 특성 'GET'을 읽을 수 없습니다 나는 angular2 관찰을 통해 웹 서비스를 얻기 위해 노력하지만, 나는 app.module.ts에서 HttpModule의 수입 오류는 Angular2가 관찰 오류 형식 오류를 얻을 :

ERROR TypeError: Cannot read property 'get' of undefined

전나무가있다. currencyService 공급자가 있습니다. 이것은 내 첫 번째 시도이다

import { Component, OnInit } from '@angular/core'; 
import { ICurrency } from './currency'; 

import { CurrencyService } from './currency.service'; 

@Component({ 
    selector: 'app-currency', 
    templateUrl: './template.html', 
    styles: [] 
}) 
export class CurrencyComponent implements OnInit { 

    currency: array<ICurrency>; 
    error: string; 

    constructor(private _currencyService: CurrencyService) { }  

    ngOnInit(){ 
     this.getCurrency(); 
    } 

    getCurrency(){ 
     this._currencyService 
      .getCurrency() 
      .subscribe(
       currency => this.currency = currency, 
       err => this.error = <any>error 
      ) 
    } 
} 

: 최종 currency.component.ts에서

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 

import 'rxjs/add/operator/map'; 

import { ICurrency } from './currency'; 

@Injectable() 
export class CurrencyService { 

    currency:Array<ICurrency>; 

    constructor(private _http: Http); 

    getCurrency(): Observable<ICurrency[]>{ 

     return this._http 
      .get('https://api.fixer.io/latest?base=PLN&symbols=EUR,USD,GBP,CZK,CHF,RUB') 
      .map(res: Response => <ICurrency[]>res.json()); 
    }; 

} 

: 다음 나는 내 currency.service.ts을 modyficated currency.ts (인터페이스) 다음을 createed Observable을 사용하고, Angular2에서 상당히 새로워졌습니다. 나는 어떤 tutoria를 기반으로 몇 가지를보고 yt를 읽었지만, 나는 심지어 어디에 실수가 될지 모른다.

+6

서비스의 생성자 이후에 중괄호가 누락되었습니다. –

+0

대단히 감사합니다. 내 바보 같은 실수였습니다. 나는지도 부분에있는 동일한 파일에서 실수를했다 – Eva

답변

0

먼저 중괄호를 추가하고 생성자에서 세미콜론을 제거하십시오.

지금, HttpClient를 http 대신를 사용하여 appModule에서

(당신이 각도 5에있는 가정) : 통화 서비스에서

import {HttpClientModule} from '@angular/common/http'; 

수입 {HttpClient를} '@ 각도/공통/http'에서;

constructor(private _http: HttpClient){} 
+0

OP는 그들이 Angular 2에 있다고 말했다. –

+0

그래서 내가 가정했다. :) 많은 사람들이 Angular 2 –