我試圖在我的 Ionic Angular 應用程式的頁面之間傳遞物件。不幸的是,我還不知道如何做到這一點。物件被傳遞到新打開的“詳細資訊”頁面一次。但是,當回傳初始“主頁”時,物件不再通過。
任何人都知道如何解決這個問題?
這是代碼:
主頁.html
<ion-content>
<div *ngIf="spaces">
<ion-item-sliding *ngFor="let space of spaces | async;">
<ion-item
(click)="openspacedetailsPage(space)"
routerDirection="forward"
detail
>
<ion-icon name="ellipse-outline" start></ion-icon>
<ion-label> {{ space.spaceName }} </ion-label>
</ion-item>
</ion-item-sliding>
</div>
主頁.ts
openspacedetailsPage(space) {
this.dataService.setSpace(space);
this.router.navigate(["tabs/space-details"]);
}
資料服務.ts
setSpace(space) {
this.space = space;
}
空間-details.page.ts
ngOnInit() {
this.space = this.dataService.space;
this.spaceName = this.space.spaceName
}
pageBack() {
this.space = {};
this.userId = "";
this.spaceId = "";
this.spaceName = "";
this.dataService.setSpace(undefined);
}
}
uj5u.com熱心網友回復:
您可以使用 QueryParms 來實作這一點。主頁.ts
openspacedetailsPage(space) {
this.dataService.setSpace(space);
this.router.navigate(["tabs/space-details"],{queryParams: {space}});
}
空間-details.page.ts
import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.queryParams
.subscribe(params => {
console.log(params);
this.space = params.space;
}
);
}
uj5u.com熱心網友回復:
這是解決方案:
空間-details.page.ts
ionViewDidEnter() {
this.space = this.dataService.space;
this.spaceName = this.space.spaceName;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383522.html
上一篇:使用Object.defineProperty時,型別缺少屬性但型別需要
下一篇:在MongoDB中等待查詢
