Ionic 3 subscribe 多次触发
Ionic 3 subscribe fires multiple times
几周来,我一直在为 Ionic 出租车应用程序中的这段代码苦苦挣扎。我的问题是:
当页面加载时,订阅会与其中的其他函数一起触发多次。在 Firebase
中修改一些旅行信息时也会出现同样的情况
是否可以只加载一次旅行和driver信息,以便屏幕只监控其他服务?
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);
}
使用订阅时,您应该始终取消订阅,以防止多次订阅。您可以尝试在变量上分配订阅并在 ionViewWillLeave() 上取消订阅
已解决。是重复的 Firebase 节点状态
几周来,我一直在为 Ionic 出租车应用程序中的这段代码苦苦挣扎。我的问题是:
当页面加载时,订阅会与其中的其他函数一起触发多次。在 Firebase
中修改一些旅行信息时也会出现同样的情况是否可以只加载一次旅行和driver信息,以便屏幕只监控其他服务?
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);
}
使用订阅时,您应该始终取消订阅,以防止多次订阅。您可以尝试在变量上分配订阅并在 ionViewWillLeave() 上取消订阅
已解决。是重复的 Firebase 节点状态