我正在编写我的第一个 Angular2 应用程序。
我有一个BankAccountService
从我的后端服务器获取数据的一个bank-account.component
,它使用这个服务来简单地显示一个未排序的 accountNumbers 列表。
通过 chrome devtool 进行调试,我能够看到数据已正确获取并存储在bankAccounts
数据成员中,但由于某种原因,ul
什么也没有显示。
银行账户服务:
import { Injectable } from '@angular/core';
import {client} from '../app.module';
import gql from 'graphql-tag';
import {ObservableQuery} from "apollo-client";
@Injectable()
export class BankAccountService{
bankAccounts: any[] = [];
constructor() {
this.queryBankAccounts();
}
queryBankAccounts(): any{
let queryObservable: ObservableQuery = client.watchQuery({
query: gql`
{
bankAccounts {
id
accountNumber
userOwners{
firstName
}
bankId
branchId
transactions{
amount
payerId
recipientId
}
}
}
`,
pollInterval: 50
});
let subscription = queryObservable.subscribe({
next: ({ data }) => {
this.bankAccounts = data;
},
error: (error) => {
console.log('there was an error sending the query', error);
}
});
}
getBankAccounts(){
return this.bankAccounts;
}
}
银行帐户组件:
import {Component} from "@angular/core";
import {BankAccountService} from "../services/BankAccountService";
@Component({
selector: 'bank-account',
template: `<ul>
<li *ngFor="let account of bankAccounts">
{{account.accountNumber}}
</li>
</ul>`,
providers: [BankAccountService]
})
export class BankAccountComponent{
bankAccounts: any[] = [];
constructor(bankAccountService: BankAccountService){
this.bankAccounts = bankAccountService.getBankAccounts();
}
}
如果有人能阐明一下,我将不胜感激,我似乎无法理解我的错误。
提前致谢