У меня есть список предметов.У каждого предмета есть идентификатор.Поэтому, когда вы нажимаете на элемент, он перемещается на страницу с подробной информацией об этом элементе.Но когда я возвращаюсь, иногда он переходит на страницу сведений без идентификатора, вызывая ошибку, поскольку невозможно получить какую-либо информацию.
Я пытался изменить события жизненного цикла navController.В настоящее время я использую ionViewDidEnter
для загрузки списка элементов и ngOnInit
при переходе на страницу сведений.
Загрузка списка элементов:
public ionViewDidEnter() {
if (this.role === "passenger") {
this.getCurrentPosition();
}
}
private getCurrentPosition() {
const loading = this.loadingCtrl.create({
content: "Obteniendo ubicación actual...",
spinner: "dots",
});
loading.present();
// Geolocation options
const positionOptions = {
enableHighAccuracy: true,
maximumAge: 600000,
timeout: 8000,
};
// Get current position
this.geolocation.getCurrentPosition(positionOptions).then((position) => {
// Get current position in google maps coords format
this.originCoords = new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude);
this.getNearbyJourneys();
// Hide loading toast
loading.dismiss();
}).catch(() => {
loading.dismiss().then(() => {
this.showErrorAlert("Error al obtener la ubicación");
});
});
}
private getNearbyJourneys() {
const body = {
lat: this.originCoords.lat(),
lng: this.originCoords.lng(),
token: this.userToken,
};
this.journey.listJourneysByOrigin(body).subscribe((d: any) => {
this.dataTrips = [];
this.items = d[0];
for (let i = 0; i < this.journeysToLoad; i++) {
if (d[0][i] != null) {
this.dataTrips.push(d[0][i]);
}
}
});
}
Страница сведений:
public ngOnInit() {
this.showLoading().then(() => {
this.getToken().then(() => {
this.getJourneyInfo(this.userToken).then(() => {
this.initMap().then(() => {
this.loading.dismiss();
}).catch((error) => {
this.loading.dismiss().then(() => {
this.showAlertConfirm(
"Vale",
error,
false,
);
});
});
});
});
});
}
private getToken(): Promise<any> {
return new Promise<any>((resolve) => {
this.tokenProvider.getToken().then((tkn: any) => {
this.userToken = tkn;
resolve();
}, (error: any) => {
this.errorHandler(error);
});
});
}
private getJourneyInfo(tkn: string): Promise<any> {
return new Promise<any>((resolve) => {
this.journey.getJourneyInfo(this.tripID, tkn).subscribe((data: any) => {
this.initializeValues(data);
resolve();
}, () => {
this.showAlertConfirm(
"Vale",
"No ha sido posible obtener la información",
true);
});
});
}
private initMap(): Promise<any> {
return new Promise<any>((resolve, reject) => {
this.plt.ready().then(() => {
const mapOptions = this.gmapsProvider.initMapOptions()
this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
this.gmapsProvider.codeAddress(
this.journeyInfo.origin.direction,
this.originIcon,
this.map,
);
this.gmapsProvider.codeAddress(
this.journeyInfo.destination.direction,
this.destinationIcon,
this.map,
);
this.gmapsProvider.calculateRouteAndDisplay(
this.map,
this.journeyInfo.origin.direction,
this.journeyInfo.destination.direction,
).then(() => {
resolve();
}).catch((error) => {
reject(error);
});
});
});
}