IONIC 4 - 角度路由参数

Edw*_*ead 4 url-routing ionic-framework angular ionic4

我正忙于将旧的 IONIC 应用程序升级到 IONIC 4 并且一切进展顺利,但是我找不到任何详细说明如何在 Angular 的路由中传递数据和使用页面的内容。

这是问题的代码片段:

旧代码

this.navCtrl.setRoot(VendorBookingDashboardProgressPage, { book: temp });

在上面的代码中,您将看到booking: temp作为附加数据的参数传递,而VendorBookingDashboardProgressPage是要导航到的参考页面。

新代码

this.navCtrl.navigateRoot('vendor/vendor-booking-dashboard-progress'); -- 这是booking: temp 参数缺失且必须包含的地方,以及硬编码的URL,其中应包含页面引用但需要字符串参数。

我可以对 URL 进行硬编码,但参数是关键,需要知道如何实现这一点。

小智 10

尝试使用这样的导航附加功能:

import { NavigationExtras } from '@angular/router';
import { NavController } from '@ionic/angular';

constructor( public navCtrl: NavController) {
}

//this should be within a function
const navigationExtras: NavigationExtras = {
      queryParams: {
        special: JSON.stringify(temp)
      }
};
this.navCtrl.navigateRoot(['vendor/vendor-booking-dashboard-progress'], 
navigationExtras);
Run Code Online (Sandbox Code Playgroud)

然后要从路由到的页面中检索它,请在页面的 ts 文件中尝试此操作:

import { ActivatedRoute, Router } from '@angular/router';

data: any;

constructor(
  public activatedRoute: ActivatedRoute,
  private router: Router) {
}

ngOnInit(){
  this.activatedRoute.queryParams.subscribe(params => {
  if (params && params.special) {
    //store the temp in data
    this.data = JSON.parse(params.special);
  }
}
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*tel 7

您可以尝试以下方法在页面之间传递数据

  1. 使用查询参数
   let navigationExtras: NavigationExtras = {
      queryParams: {
        special: JSON.stringify(this.user)
      }
    };
    this.router.navigate(['details'], navigationExtras);
Run Code Online (Sandbox Code Playgroud)
  1. 服务和解析功能(合法)
  setData(id, data) {
    this.data[id] = data;
  }

  getData(id) {
    return this.data[id];
  }
Run Code Online (Sandbox Code Playgroud)
  1. 使用 Extras 状态(自 Angular 7.2 起新增)
    let navigationExtras: NavigationExtras = {
      state: {
        user: this.user
      }
    };
    this.router.navigate(['details'], navigationExtras);
Run Code Online (Sandbox Code Playgroud)

参考:https : //ionicacademy.com/pass-data-angular-router-ionic-4/