【问题标题】:Passing Data between pages from url从 url 在页面之间传递数据
【发布时间】:2019-02-11 02:19:48
【问题描述】:

我正在从事 ionic 4 项目。我的项目是使用输入搜索从 url 获取数据 json。我希望在用户单击任何项​​目时传递数据以向他们显示另一个页面以获取更多详细信息。我尝试使用此代码,但我得到未定义的值!

这段代码正在进行搜索操作

export class FlightsearchPage {
  public search = ''
  public flight : any
  filterItems:any;
  callsign : any;
  constructor(private http: HTTP, public loadingController: LoadingController, private nav : NavController) {

    this.addThemFunction
  }

  keyPressed(event: any) { /
    console.log(event.target.value); 
  this.addThemFunction(event.target.value);
  }

 async addThemFunction(search){

  this.http.get('/web/find?query='+search+'', {}, {})
  .then(data => {

    const parsed = JSON.parse(data.data);
    this.flight = parsed.results;
    this.filterItems= this.flight;
    this.callsign = this.flight.detail.flight

    /// here l am try to pass value callsign to page details /// 

    this.nav.navigateForward(`/flightserachdetails/${this.callsign}`)

    }), err=>{
    }
  }


    }

详情搜索

 <ion-content padding>
      <ion-item>
          <ion-label position="floating">Enter your flight number</ion-label>
          <ion-input [(ngModel)]="search" (keyup)="keyPressed($event)" placeholder="Ex : iaw556"></ion-input>
        </ion-item>

  <ion-item *ngFor="let item of flight"  routerDirection="forward">
      <ion-label>
        <ion-text color="primary">
            <h3 [routerLink]="['/flightserachdetails', id]">{{item.label}}</h3>
          </ion-text>
          <p>{{item.name}}</p>
      </ion-label>
    </ion-item>

</ion-content> 

这段代码正在从上面的代码中获取数据

export class FlightserachdetailsPage {

  public flight : any
  callsignId = null

  constructor(private http: HTTP, public loadingController: LoadingController, private nav: NavController,private activatedRoute: ActivatedRoute) {


    this.callsignId = this.activatedRoute.snapshot.paramMap.get('id')           





}

用于显示来自上面另一个页面的数据。

<ion-content padding>
   {{callsignId}}
   {{flight.request.fetchBy}}
</ion-content>

json 响应

{
  "results": [
    {
      "id": "IA107",
      "label": "IA107",
      "detail": {
        "callsign": "IAW107",
        "flight": "IA107", 
       "fetchBy": "IA107"
      },
      "type": "schedule",
    }
  ]
}

我很抱歉混乱代码。请给我任何建议或给我另一个代码示例?

【问题讨论】:

  • 在现有服务中编写服务或方法/属性以通过其 id 获取对象/项目,并在导航到下一页时将对象/项目的 id 传递到 url 并调用此服务方法在下一页上加载的组件中通过 id 获取该对象。我认为这更容易。

标签: angular ionic4


【解决方案1】:

您正在同时进行查询和导航,因此请将其从 addThemFunction(search) 中删除。

async addThemFunction(search){

    this.http.get('/web/find?query='+search+'', {}, {})
       .then(data => {

          const parsed = JSON.parse(data.data);
          this.flight = parsed.results;
          this.filterItems= this.flight[0];
          this.callsign = this.flight.detail.flight

        }
     }
}

您需要从addThemFunction(search) 函数中删除导航并创建一个新的导航,以便在单击项目时进行导航。

navigate(id){
    this.nav.navigateForward(`/flightserachdetails/${id}`);
}

并从 h3 中删除 routerLink 并在单击项目时调用 navigate() 函数并在该函数中传递 id

<ion-item *ngFor="let item of flight"  routerDirection="forward">
  <ion-label>
    <ion-text color="primary">
        <h3 (click)="navigate(item.id)">{{item.label}}</h3>
      </ion-text>
      <p>{{item.name}}</p>
  </ion-label>
</ion-item>

传递完整的对象:

`this.router.navigate(['flightserachdetails'], item);

在下一页:

constructor(private activatedRoute: ActivatedRoute) {
   this.activatedRoute.queryParams.subscribe(resp => {
       console.log(resp);
   });

【讨论】:

  • 他不工作,因为我使用的是 Ionic/Angular 4,所以我必须使用 this.nav.navigateForward(/flightserachdetails/${this.flight.detail.flight}`。它是 Angular 路由器的方法
  • 我认为问题在于您的结果是数组尝试this.filterItems= this.flight[0];
  • 当我搜索 nay 查询工作正常时。但是当我点击项目时,我在页面详细信息中得到了未定义的值
  • 同样的问题
  • 很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2016-11-10
  • 2011-04-02
相关资源
最近更新 更多