【问题标题】:Ionic3 publish event throwing exceptionIonic3 发布事件抛出异常
【发布时间】:2019-08-29 12:07:17
【问题描述】:

有没有人在 Ionic 3 中观察到发布事件时抛出异常?

编辑:经过仔细检查,异常与注入的Events 在构造函数首次调用后的某个时间变为undefined 有关。我想知道是否有我不知道的先决条件,例如为了注入Events,它必须在一个组件内完成吗?在我的情况下,我正在尝试从提供者发布事件:

import { Injectable } from '@angular/core';
import { Events } from 'ionic-angular';
...
@Injectable()
export class MyProvider {
...
  constructor(public http: HttpClient,
    private events: Events,
  ) {
    console.log('Hello MyProvider Provider');
    console.log(`${ this.events } ${ typeof this.events }`) // [object Object] object
...
  private onUpdate(p: IAPProduct) {
    console.log(`${ this.events } `) // undefined
    this.events.publish('my event', p) // TypeError: undefined is not an object (evaluating 'this.events.publish')
  }

唯一可能是一个因素的其他细节是 onUpdate 在其构造函数执行期间被用作回调。

提前感谢您的宝贵时间。

【问题讨论】:

    标签: ionic3


    【解决方案1】:

    解决了。

    回调引用了错误的this,我用bind解决了这个问题:

        this.store.when(this.productId).updated(this.onUpdate.bind(this)) // from the constructor
    ...
    
      private onUpdate(p: IAPProduct) {
        console.log(`${ this.events } `) // [object Object]
        this.events.publish('my event', p) // no longer throws exception :)
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 2014-03-21
      • 2013-05-24
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多