Angular2 获取可观察的错误类型错误:无法读取未定义的属性"get"



我尝试通过Angular2可观察到WebService,但我有错误

错误typeerror:无法读取未定义的属性

firs i在app.module.ts中导入httpmodule。我有CurrencyService的提供商。接下来,我创建货币。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.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
            )
    }
}

这是我第一次尝试使用可观察到的,我在Angular2中很新。我基于一些Tutoria,在YT上观看几个并阅读,但我什至不知道在哪里可能是错误的。

首先请添加卷曲括号并从构造函数中删除半隆。

现在,使用httpclient代替http(假设您在Angular 5上)

在您的AppModule中:

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

在您的货币服务中

来自'@angular/common/http'的导入{httpclient};

constructor(private _http: HttpClient){}

相关内容

  • 没有找到相关文章

最新更新