![](/img/trans.png)
[英]How to set data into a service and the get in another page Ionic 4/5
[英]Ionic 4 How to get data from another page
我正在瀏覽頁面並將數據發送到頁面。 我想知道如何獲取發送到頁面的數據? 。
示例:在ionic 3中,我們推送頁面並發送數據,如下所示
this.navCtrl.push(Page, {x:x})
在其他頁面上,我們從navParams獲取日期,例如
this.navParams.get('x');
我需要知道如何在ionic 4中獲取數據嗎? 我正在這樣導航頁面和數據
details(y){
this.navCtrl.navigateForward('/packagedetails',{y:y});
}
您可以使用navigaionExtras實現此目的
第1頁:
let navigationExtras: NavigationExtras = {
state: {
x: x
}
};
this.router.navigate(['details'], navigationExtras);
第2頁:
constructor(private route: ActivatedRoute, private router: Router) {
this.route.queryParams.subscribe(params => {
if (this.router.getCurrentNavigation().extras.state) {
this.data = this.router.getCurrentNavigation().extras.state.x;
}
});
}
他們有很多方法可以做到這一點。 這是三個:
這是一個頁面參數,另一個參數,但是如果用戶關閉應用程序,則不會保存任何內容(不確定是否為100%,但是如果我記得很好,則不會):
第1頁:
import {Router} from "@angular/router"
@Component(thingsThatIsIt)
export class Page1Page{
constructor(private router: Router,OthersThingsInYourConstructor){thingsThatIsIt}
FunctionToGoOnPage2(someParameters) //someParameters is an JS object (like JSON for exemple)
{
this.router.navigate(["Page2",someParameters])
}
}
第2頁:
import {ActivatedRoute} from "@angular/router"
@Component(thingsThatIsIt)
export class Page2Page{
constructor(private route: ActivatedRoute,OthersThingsInYourConstructor)
{
this.route.params.subscribe(params => {
console.log(params)
FunctionThatDealWithParameters(params)
})
}
FunctionThatDealWithParameters(someParameters) //someParameters is an JS object (like JSON for exemple)
{
//code to use your parameters
}
}
這是許多其他頁面的一頁,但是如果用戶關閉應用程序,則不會保存任何內容:
提供服務: ionic generate service
在此服務中,創建一些屬性以及一些get和set方法。
在需要服務的頁面中,獲取或設置方法,只需導入它,然后調用get / set方法(順便說一句)
這是許多頁面的一頁,並且即使用戶關閉了他的應用程序,也保存了所有內容:
使用離子本地存儲
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.