【问题标题】:Navigating & Passing Data Between Pages Ionic 2在页面之间导航和传递数据 Ionic 2
【发布时间】:2017-04-04 14:30:20
【问题描述】:

我在使用 Ionic2 在页面之间导航时遇到了一些问题。

我有一个从 data.json 获得的数据列表

这是列表

我想了解详情:

示例 - 来自“A”

我的数据/Example.Json 上的数据

 {
  "title": "A",
  "lat": 2.323733,
  "lon": 64,546465
},

所以只要我点击标题,我就想在新页面上获取标题、纬度和经度。

app.modules.ts

    import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { MapPage } from '../pages/map/map';
import { ListPage } from '../pages/list/list';
import { DetailPage } from '../pages/detail/detail';
import { Locations } from '../providers/locations';
import { GoogleMaps } from '../providers/google-maps';
import { Connectivity } from '../providers/connectivity';
declare var google: any;

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},Locations,GoogleMaps, Connectivity]
})
export class AppModule {}

    import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { MapPage } from '../pages/map/map';
import { ListPage } from '../pages/list/list';
import { DetailPage } from '../pages/detail/detail';
import { Locations } from '../providers/locations';
import { GoogleMaps } from '../providers/google-maps';
import { Connectivity } from '../providers/connectivity';
declare var google: any;

@NgModule({
  declarations: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage,
    MapPage,
    ListPage,
    DetailPage
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},Locations,GoogleMaps, Connectivity]
})
export class AppModule {}

PageA.ts(数据列表)

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Locations } from '../../providers/locations';
import { DetailPage } from '../detail/detail';


@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})
export class ListPage {


  constructor(public navCtrl: NavController, public locations: Locations) {

  }

  ionViewDidLoad() {
    console.log('Test Si marche');


  }

  viewLocation(event,location) {
    this.navCtrl.push(DetailPage, {
      "title":location.title,
      "longitude":location.longitude
    })
  }

}

页面 B(我想在单击列表中的某些内容后立即获取详细信息)

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {NavParams} from "ionic-angular";
import { Locations} from '../../providers/locations';
import { ListPage } from '../list/list';





@Component({
    selector: 'page-detail',
    templateUrl: 'detail.html',
    providers: [ Locations ],
    entryComponents:[ ListPage ]
})


export class DetailPage {

    title: string
    latitude: string
    navParams: NavParams

    constructor(navParams: NavParams,public navCtrl: NavController) {
        this.navParams = navParams
        this.title = this.navParams.get('title');
        this.latitude = this.navParams.get('latitude');
    }

    ionViewDidLoad(err) {
        console.log(err);


    }
    goBack() {
        this.navCtrl.pop();
    }

}

listA.html

 <ion-header>

    <ion-navbar color="secondary">
        <ion-title>List</ion-title>
    </ion-navbar>

</ion-header>


<ion-content>

    <ion-list no-lines>

        <ion-item *ngFor="let location of locations.data">

            <ion-item (click)="viewLocation($event, locations)">{{locations.title}}</ion-item>
            <ion-avatar item-left>
                <ion-icon name="pin"></ion-icon>
            </ion-avatar>
            <!--<img src="./src/pics/mus.png"/>-->
            <h2>{{location.title}}</h2>
            <p>{{location.distance*1.609}} Km</p>
        </ion-item>

    </ion-list>

</ion-content>

数据.json

  [
  { "title": "Cat", "longitude": "14,57" },
  { "title": "Bird", "longitude": "17.45" },
  { "title": "Spider", "longitude": "19,12" }
]

【问题讨论】:

  • 到目前为止你尝试过什么?您应该展示您拥有的代码(与此问题相关),并告诉我们它是如何不工作的。
  • @Schlaus 为例:constructor(public navCtrl: NavController) { this.list = [ { Name: 'A', lat: 154564, lng:2.454}, } public Listitem(l) { this.navCtrl.push(DetailsPage,{item:l});我的问题是我的列表存储在 data.json 上的不同文件中,我想从那里获取并显示详细信息
  • 那么,您想从不同的文件中获取数据,然后在导航期间将其作为参数发送..?
  • @RajaYogan 是的先生!我有一个列表:*A *B *C 只要我点击 A,我就会得到 A 的详细信息(标题 + 纬度 + 经度),它存储在 Data.json 中
  • 好吧,如果列表中的数据没有改变,那么你可以简单地使用生命周期事件触发器,即 ionViewDidEnter() 来加载与 pageB.ts 文件中的标题 A 相关的数据在 pageB .ts constructor() {} ionViewDidEnter() { //从你的文件中加载数据 }

标签: json ionic2


【解决方案1】:

如果你想将数据从一个页面传递到另一个页面,你可以

1) 通过 JSON.stringify(store) 和 JSON.parse(retrieving) 方法以 SQL 格式存储您的 JSON 数据。

这意味着您将数据字符串化,在存储在 SQL 表中并从下一页检索后,您执行 JSON.parse 以取回您的 JSON 对象。

您可能想阅读以下有关如何在 SQL 中存储数据的文章。 SQL Ionic 2

JSON Parse

JSON Stringify

或者,2)您可以使用 navController 将数据从一个页面“推送”到另一个页面。一个缺点是,如果您的 JSON 数据很大,您将不得不慢慢迭代。

一个简单的例子是:

this.navController.push(ThePageNameYouWannaGo, {
      firstPassed: "firstData",
      secondPassed: "secondData",
      thirdPassed: "thirdData",
});


//On the next page (at your constructor),

Constructor(....,NavParams,.....){

  this.first = navParams.get("firstPassed");
  this.second= navParams.get("secondPassed");
  this.third= navParams.get("thirdPassed");

//the value would be stored inside this.first, this.second respectively 

对于您的情况,我会推荐方法 1,因为我不知道您的 JSON 文件有多大,并且在您的 JSON 文件更改时让您的生活更轻松。

【讨论】:

  • 谢谢你的回答,我收到了这个错误:Cannot read property 'parameters' of undefined at ReflectionCapabilities.parameters
  • @Taieb 你用的是什么方法?
  • 当我想在页面 B 上显示详细信息时,我现在设法纠正它,我得到了一个空白页面,我做了 console.log(this.title); ==>什么都没有
  • 如果您使用其他方法打印控制台,请尝试在此行中添加:" var self = this; console.log(self.title); "
  • this.navParams = navParams this.title = this.navParams.get('title'); this.latitude = this.navParams.get('latitude');未定义它在我的 PageB 上,要编辑主题,所以你可以检查代码
猜你喜欢
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 2018-04-23
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多