离子3:事件不适用于app组件

时间:2018-04-12 15:55:03

标签: angular ionic-framework ionic3 angular5

我想将应用程序组件中的事件发布到将要订阅该事件的任何页面。

(SRC /应用/ app.component.ts)

import { Component, ViewChild } from '@angular/core';
import { Nav, App, Events } from 'ionic-angular';

@Component({
    templateUrl: 'app.html'
})

export class MyApp{

    logged: boolean = true; // Just for example    

    constructor(app: App, events: Events){
        this.events.publish('app:user', this.logged);
    }
}

(SRC /页/沙箱/ sandbox.ts)

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Events } from 'ionic-angular';

@IonicPage()
@Component({
    selector: 'page-sandbox',
    templateUrl: 'sandbox.html',
})

export class SandboxPage{

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public events: Events) {

        console.log('Subscribing the event!');
        this.events.subscribe('app:user', logged => {
            console.info(logged);
        });
    }
}

什么都没发生。我没有得到控制台日志输出,也没有任何错误。有什么东西我不见了吗?

1 个答案:

答案 0 :(得分:2)

不知何故,我让它使用这种方法,但我不得不相信应该有更好的东西。

(SRC /应用/ app.component.ts)

return models.Voucher.update({
    sentAt: moment().format(),
    usedIn: args.sentTo,
  }, { where: { id: args.id } })
    .then(resp => (
      models.Voucher.findOne({ where: { id: args.id } })
        .then((voucher) => {
          pubsub.publish(VOUCHER_SENT, { voucherSent: voucher, estId: voucher.usedIn });
          return resp;
        })
    ))

(SRC /页/沙箱/ sandbox.ts)

this.events.subscribe('app:getUser', () => {
    console.log('Publish the event!');
    this.events.publish('app:sendUser', true);
});