我刚开始学习Angular2,我做了一些教程。我必须根据界面显示服务数据。我有问题。
我认为问题来自TypeScript界面,但是,因为它对我来说是新事物,我甚至不知道在哪里寻找错误。
currency.ts(接口):
export interface ICurrency {
base: string;
date: string;
rates: object;
}
currency.service:
import { Injectable } from '@angular/core';
import { ICurrency } from './currency';
@Injectable()
export class CurrencyService {
currency:Array<ICurrency>;
getCurrency(){
this.currency = [{
base: "base1111",
date: "11111",
rates: {a:"a",b:"b"}
},
base: "base2222",
date: "222222",
rates: {a:"c",b:"d"}
}]
};
}
currency.component
import { Component, OnInit } from '@angular/core';
import { CurrencyService } from './currency.service';
import { ICurrency } from './currency';
@Component({
selector: 'app-currency',
template: `
<p>
currency works!
</p>
`,
styles: []
})
export class CurrencyComponent implements OnInit {
constructor(private _currencyList: CurrencyService) { }
getCurrency(){
this.currency = this._currencyList.getCurrency();
console.log(this.currency);
}
ngOnInit(){
this.getCurrency();
}
}
Consola应显示该对象,但我收到错误
AppComponent.html:1 ERROR ReferenceError:未定义base at CurrencyService.getCurrency(currency.service.ts:19) 在CurrencyComponent.getCurrency(currency.component.ts:22) at CurrencyComponent.ngOnInit(currency.component.ts:27) 在checkAndUpdateDirectiveInline(core.js:12095) at checkAndUpdateNodeInline(core.js:13598) at checkAndUpdateNode(core.js:13541) 在debugCheckAndUpdateNode(core.js:14413) 在debugCheckDirectivesFn(core.js:14354) 在Object.eval [as updateDirectives](AppComponent.html:3) at Object.debugUpdateDirectives [as updateDirectives](core.js:14339)
我真的不知道我哪里弄错了。我在app.modules中添加了提供者CurrencyService。请帮忙,我想学习Angular2,所以我想了解我的错误。