如何在ionic2的pop方法中传递参数

Aka*_*Rao 14 javascript ionic2 angular

我尝试在ionic2的push方法中传递参数.像这样

this.nav.push(SecondPage, {
    thing1: data1,
    thing2: data2
});
Run Code Online (Sandbox Code Playgroud)

但有没有办法在pop()中传递参数.

Pra*_*ant 15

这就是我在离子3中实现它的方法,并且发现它更容易.

我们pop()的页面

 this.navCtrl.getPrevious().data.thing1 =data1;
 this.navCtrl.getPrevious().data.thing2 =data2;
 this.navCtrl.pop();
Run Code Online (Sandbox Code Playgroud)

pop()后的页面:

public ionViewWillEnter() {
     this.thing1 = this.navParams.get('thing1')|| null;
     this.thing2 = this.navParams.get('thing2')|| null;
} 
Run Code Online (Sandbox Code Playgroud)


akz*_*z92 14

我建议你使用Events.您所要做的就是在父页面上订阅一个事件,然后在传递所需数据的子项上发布事件:

// taken from the docs
import { Events } from 'ionic-angular';

constructor(public events: Events) {}

// first page (publish an event when a user is created)
function createUser(user) {
  console.log('User created!')
  events.publish('user:created', user);
}

// second page (listen for the user created event)
events.subscribe('user:created', (userEventData) => {
  // userEventData is an array of parameters, so grab our first and only arg
  console.log('Welcome', userEventData[0]);
});
Run Code Online (Sandbox Code Playgroud)


Ank*_*ngh 1

更新:它本来应该起作用,但它不起作用


好像有| 请参阅文档参考|

pop(opts)接受一个类型参数object

所以

退一步

this.nav.pop({
    thing1: data1,
    thing2: data2
});
Run Code Online (Sandbox Code Playgroud)

并转到历史堆栈中的特定视图

this.nav.popTo(SecondPage, {
    thing1: data1,
    thing2: data2
});
Run Code Online (Sandbox Code Playgroud)

并转到堆栈的根

this.nav.popToRoot({
    thing1: data1,
    thing2: data2
});
Run Code Online (Sandbox Code Playgroud)

检索参数 (我想这应该可行。未经测试!)

export class SecondPage{
 constructor(params: NavParams){
   this.params = params;

   console.log(this.params.get('thing1'));
 }
}
Run Code Online (Sandbox Code Playgroud)

  • 根据链接到的文档,可以传递到“pop”方法的唯一对象是“NavOpts”对象。[您可以在同一页面的下方阅读 NavOptions 的文档](http://ionicframework.com/docs/v2/api/components/nav/NavController/#navoptions)。它本质上是关于导航本身的动画、缓动和方向的一些选项。将参数传递给“pop”是当前的功能请求。您可以关注 Ionic 的 [Github](https://github.com/driftyco/ionic/issues/5724) 上的讨论。 (6认同)