简体   繁体   English

Angular2获取可观察到的错误TypeError:无法读取未定义的属性“ get”

[英]Angular2 get observable ERROR TypeError: Cannot read property 'get' of undefined

I try to get webservice via angular2 observable, but I have error 我尝试通过angular2观察到Web服务,但出现错误

ERROR TypeError: Cannot read property 'get' of undefined 错误TypeError:无法读取未定义的属性'get'

Firs I imported HttpModule in app.module.ts. 首先,我在app.module.ts中导入了HttpModule。 I have providers for currencyService. 我有currencyService的提供者。 Next I createed currency.ts (interface) Next I modyficated my currency.service.ts: 接下来,我创建了currency.ts(界面)接下来,我修改了currency.service.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());
    };

}

In the end currency.component.ts: 最后,currency.component.ts:

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

This is my first try to use observable, and I'm quite new in Angular2. 这是我第一次尝试使用可观察的方法,而我在Angular2中还是一个新手。 I based on some tutoria, watch on yt several and read too, but I even don't know where could be the mistake. 我以一些托里亚大学为基础,还看了几本书,还读了,但是我什至不知道哪里出了问题。

Firstly please add curly braces and remove semicolon from your constructor. 首先,请添加花括号并从构造函数中删除分号。

Now, use HttpClient instead of Http (Assuming you are on Angular 5) 现在,使用HttpClient而不是Http(假设您使用的是Angular 5)

In your appModule: 在您的appModule中:

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

In your Currency Service 在您的货币服务中

import {HttpClient} from '@angular/common/http'; 从'@ angular / common / http'导入{HttpClient};

constructor(private _http: HttpClient){}

暂无
暂无

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

相关问题 Angular2:可观察的flatMap错误无法读取未定义的属性“ subscribe” - Angular2: Observable flatMap error Cannot read property 'subscribe' of undefined angular2:错误:TypeError:无法读取未定义的属性“...” - angular2: Error: TypeError: Cannot read property '...' of undefined 错误TypeError:无法读取未定义的属性&#39;get&#39; - ERROR TypeError: Cannot read property 'get' of undefined TypeError:无法在Angular2 Observable中读取null的属性“next” - TypeError: Cannot read property 'next' of null in Angular2 Observable 无法读取未定义的属性“get”的 Angular 5 错误 - Angular 5 error for cannot read property 'get' of undefined Angular2 - 单元测试Observable错误“无法读取未定义的属性&#39;subscribe&#39;” - Angular2 - Unit testing Observable error “Cannot read property 'subscribe' of undefined” 未捕获(承诺):类型错误:无法读取 angular2+ 中未定义的属性“get” - Uncaught (in promise): TypeError: Cannot read property 'get' of undefined in angular2+ 类型错误:无法读取未定义的属性“get”(Angular getter) - TypeError: Cannot read property 'get' of undefined (Angular getters) Angular6-FormControlName-TypeError:无法读取未定义的属性“ get” - Angular6 - FormControlName - TypeError: Cannot read property 'get' of undefined angular http TypeError:无法读取未定义的属性“get” - angular http TypeError: Cannot read property 'get' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM