我在Ionic出租车应用程序中苦苦挣扎了几个星期。我的问题是:
页面加载时,订阅会与页面中的其他功能一起触发多次。在Firebase中修改某些旅行信息时,也会发生同样的情况
是否可以只加载一次旅行和驾驶员信息,以便屏幕仅监视其他服务?
Traveling.ts:
export class TravelingPage {
driver;
trip;
...
constructor(
public nav: NavController,
...
) {
...
if (this.navParams.get('tripId')) {
this.tripId = this.navParams.get('tripId')
}
else {
this.tripId = this.tripService.getId();
}
}
ionViewDidLoad() {
this.tripService.getTrip(this.tripId).take(1).subscribe(snapTrip => {
this.driverService.getDriver(snapTrip.driverId).take(1).subscribe(snapDriver => {
// load trip data
this.trip = snapTrip;
// load driver data
this.driver = snapDriver;
// watchTrip
this.watchTrip(this.trip.id);
// start map
this.loadMap();
// check if trip is already paid
if (!this.trip['paymentData'] && this.orderStatus == false) {
// pay Trip
this.tripService.doOrder(this.trip);
} else {
console.info('Paid already. Do nothing');
}
}
});
})
}
...
}
TripService.ts:
getTrip(id) {
return this.db.object('trips/' + id);
}
答案 0 :(得分:1)
使用订阅时,应始终取消订阅,以防止进行多次订阅。您可以尝试在变量上分配订阅,并在ionViewWillLeave()上取消订阅
答案 1 :(得分:0)
已解决。是重复的Firebase节点状态