【问题标题】:Angular 2 - subscribing to Observable.fromEvent error: "Invalid event target"Angular 2 - 订阅 Observable.fromEvent 错误:“无效的事件目标”
【发布时间】:2017-07-16 05:39:42
【问题描述】:

我在尝试订阅 Observable 时遇到一个奇怪的错误。

这里是出现问题的代码的淡化版本:

import {Component, Input, OnInit, ViewChild} from '@angular/core';
import Rx from 'rxjs/Rx';

@Component({
  selector: 'action-overview-description',
  template: require('./actionOverviewDescription.html')
})
export class ActionOverviewDescription  {
  @ViewChild('button') button;

  constructor() {}
  
   ngOnInit() {

    let buttonStream$ = Rx.Observable.fromEvent(this.button, 'click')
        .subscribe(res => console.log(res));

  }
}
<button #input>Button</button>

我在控制台中得到的错误是:

无效的事件目标

该错误仅在我订阅流时出现。如果我只创建它但不订阅,则没有错误。如果我 console.log 流它似乎有一个订阅成员。

我尝试在谷歌上搜索错误,但似乎找不到任何解释的地方。

我想我正在使用 Rxjs 4.0.5(根据 npm rxjs --version)。

【问题讨论】:

  • 你能把你的html代码也给我们看看吗?
  • ngAfterViewInit
  • 我正在尝试 ngAfterViewInit。这似乎没有什么不同。您想查看 HTML 的任何特定部分吗?模板被简化为“
  • 是否加载了模板?里面有错误吗?
  • @ViewChild('button') 应该是ViewChild('input')

标签: angular rxjs observable


【解决方案1】:

如果您想在ngOnInit 事件中访问它,那么您必须使用ViewChild{ static: true } 属性,如下所示:

import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';
import { Observable, fromEvent } from 'rxjs';

@Component({
  template: '<button #input>Button</button>'
})
export class ActionOverviewDescription implements OnInit {
  @ViewChild('input', { static: true }) button: ElementRef;

  ngOnInit() {
    let buttonStream$ = Observable.fromEvent(this.button.nativeElement, 'click')
        .subscribe(res => console.log(res));

  }
}

【讨论】:

  • 我正在努力在 ngOnInit 中附加到 div 的滚动事件。这个工作示例帮助我实现了目标。谢谢!
  • 很高兴它有帮助:)
【解决方案2】:

问题在于您正在使用的生命周期挂钩。调用 ngOnInit 时,该元素尚未在 DOM 中创建。相反,您应该使用ngAfterViewInit

你能试试下面的代码吗:

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { Observable, fromEvent } from 'rxjs';

@Component({
  template: '<button #input>Button</button>'
})
export class ActionOverviewDescription implements AfterViewInit {
  @ViewChild('input') button: ElementRef;

  ngAfterViewInit() {
    let buttonStream$ = Observable.fromEvent(this.button.nativeElement, 'click')
        .subscribe(res => console.log(res));

  }
}

【讨论】:

  • 好吧,在摆弄了一段时间后,它现在可以工作了!我认为错误可能在于我导入 Rxjs-stuff 的方式。也许缺少“import 'rxjs/add/observable/fromEvent'。如果是这种情况,这只是一个相当不具描述性的错误消息。我也觉得 ElementRef 已被弃用或什么......无论如何 - 它现在可以工作了!谢谢你去 AngularFrance 寻求答案。
  • 好的,很好。 :) ElementRef 是marked as stable in the API,我不认为它已被弃用。无论如何,它只是一个类型注释。你可以用any 替换它,代码也一样。仅供参考,我认为您的代码中的主要错误是针对 this.buttonthis.button.nativeElement
  • 这对我不起作用。我最终将 button 定义为 type : any,然后使用 Observable.fromEvent(this.button._elementRef.nativeElement , 'click') - 我在 Ionic 3 项目的上下文中使用。它在幕后使用 Angular 4/..
  • 那是因为您的按钮可能是MatButton、@JGFMK 或类似的类型,而不是ElementRef
  • 顺便说一句,ViewChild 将始终在 ngOnInit() 中可用。对于 Viewchildren,您只需要 afterViewInit,以及 *ngFor、ngIf 或 ng-template 中的 ViewChild。在 Angular 的源代码中,ViewChild 被认为是“静态的”,并且必须在调用 ngOnInit 之前可用(搜索“ViewChild static ngOnInit”)github.com/angular/angular/issues/21800#issuecomment-360799520
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 2023-03-09
  • 2016-10-09
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多