【问题标题】:Angular2 EventEmitter does not complete after invoking .error调用 .error 后 Angular2 EventEmitter 未完成
【发布时间】:2016-07-17 13:35:46
【问题描述】:

我有一个 EventEmitter 通知用户,该用户通知组件应用程序的状态已更改。

此事件通知尝试验证的用户是否成功。

private emitAuthStatus(success: boolean) {
    if (success) {
        this.locationWatcher.emit({
            authenticated: this.authenticated,
            token: this._authData.token,
            expires: this.expires
        });
    } else {
        this.locationWatcher.error();
    }

    this.locationWatcher.complete();
}

现在当我拨打.emit 后接.complete 时,一切正常。 但是如果我调用.error.complete 会抛出一个ObjectUnsubscribedError

.error 的用途是什么?我在这段代码中做错了什么?

【问题讨论】:

  • 但是为什么在这里调用 error() 和 complete() 呢?您应该在组件中订阅locationWatcher observer 的地方调用/使用它们。
  • 错误是否意味着事件出现问题,或者事件引发了特殊类型的事件?当我查看文档时,它似乎引发了一种特殊类型的事件,但肯定不应该意味着 EventEmitter 不再订阅?
  • 我认为根本不应该使用error()complete()EventEmitter 当前扩展了 Observable,但这是一个实现细节,预计会随时更改而不会出现任何弃用阶段。 EventEmitter 仅用于 @Output() 仅在组件中,并且仅使用方法 emit()
  • 用使用它的组件更新你的代码。所以我们可以告诉你更多。
  • 谢谢两位,我已将代码更改为仅使用 .emit 并在发出的对象中发送失败信息。

标签: angular rxjs eventemitter


【解决方案1】:

我已将我的代码更改为不使用.error.complete,它们不打算用于传输信息。

private emitAuthStatus() {
    this.locationWatcher.emit({
        // The .authenticated allow us to know if the EventEmitter is a success
        authenticated: this.authenticated, 
        token: this._authData.token,
        expires: this.expires
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2018-02-20
    • 2016-05-22
    相关资源
    最近更新 更多