我正在使用 ionic 3。我在更新视图中的库存数据时遇到问题。我可以在我的组件上经常获得更新,我可以在我的控制台中看到有关更改的信息,但不会在我的视图中更新这些更改。但我的观点是更新罕见的情况。如果组件中发生 10 次更新,则意味着我认为有 1 次更新。我编写了如下代码,服务:
@Injectable()
export class MarketService{
fncallback:any;
stocks: string[][];
constructor( ) {
this.initStocks();
this.subscribeStocks();
}
subscribeStocks() {
this.lsClient = new LightstreamerClient("http://localhost:8080", "WBSTOCKLIST_REMOTE");
this.lsClient.connectionSharing.enableSharing("WBCommonConnection", "ATTACH", "CREATE");
this.lsClient.connect();
this.subscription = new Subscription("MERGE", this.itemNames, this.fieldNames);
this.subscription.setDataAdapter("WBQUOTE_ADAPTER");
this.subscription.addListener({
onItemUpdate: (updateObject) => {
var itemName = updateObject.getItemName();
updateObject.forEachChangedField((fieldName, fieldPos, val) => {
var itemIndex = this.itemNames.indexOf(itemName);
var fieldIndex = this.fieldNames.indexOf(fieldName);
console.assert(fieldIndex != -1);
console.assert(itemIndex != -1);
this.stocks[itemName][fieldName] = val;
});
this.fncallback(this.stocks);
}
});
this.lsClient.subscribe(this.subscription);
}
setcallback(fn){
this.fncallback = fn;
}
}
我的组件代码看起来像,
export class MarketPage {
baserates:any;
constructor(public marketService: MarketService) {
}
ionViewDidLoad() {
console.log( 'ionViewDidLoad MatketPage' );
this.marketService.setcallback((data)=>{
this.baserates = data;
console.log(this.baserates); //This update happens frequently
});
}
}
我的观点看起来像,
<ion-row class="light-border-btm align-items" *ngIf="baserates">
<ion-col col-3>Gold</ion-col>
<ion-col col-3>{{ baserates['SPOT-GOLD']['bid'] }}</ion-col>
<ion-col col-3>{{ baserates['SPOT-GOLD']['ask'] }}</ion-col>
<ion-col col-3>H:{{ baserates['SPOT-GOLD']['high'] }}<br/>L:{{ baserates['SPOT-GOLD']['low'] }}</ion-col>
</ion-row>
但我的观点是没有像在组件 baserates 字段中获得更新那样频繁更新。你能帮助任何人吗?我使用 Lightstreamer 来获取更新率。