angular - instance[output.propName].subscribe 不是由 polyfill 生成的函数

标签 angular typescript karma-jasmine

我遇到了一个奇怪的问题。我研究了这个错误,它通常似乎与 @OutputEventEmitter 的使用有关,但这里并非如此。

如果我有特定的 polyfill,我的应用程序会出现以下错误:

TypeError: instance[output.propName].subscribe is not a function
    at createDirectiveInstance (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:12319:71)
    at createViewNodes (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:13776:38)
    at callViewAction (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:14210:1)
    at execComponentViewsAction (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:14119:1)
    at createViewNodes (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:13804:1)
    at createRootView (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:13665:1)
    at callWithDebugContext (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:15090:26)
    at Object.debugCreateRootView [as createRootView] (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:14373:1)
    at ComponentFactory_.webpackJsonp.../../../core/esm5/core.js.ComponentFactory_.create (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/core.js:11260:26)
    at initComponent (http://localhost:9876/_karma_webpack_/webpack:/Users/stuart/localhost/projects/tt-new/work-group/node_modules/@angular/core/esm5/testing.js:1137:1)

polyfill如下:

Object.prototype.clone = function(deep = false) {
    return (deep)
        ? JSON.parse(JSON.stringify(this)) // Deep clone - copies object property values in full.
        : Object.assign({}, this);         // Shallow clone - copies property values (including object references rather than object).
};

谁能帮我理解为什么这会在 createDirectiveInstance 中导致错误?如果我注释掉这段代码,错误就会消失。不仅仅是有一个 polyfill 会导致问题 - 还有另一个,它添加了 Array.prototype.upsert,它不会导致问题。

最佳答案

我刚刚遇到这个问题,检查我编写的代码后发现这是由于错误导入 EventEmitter 类(由 visual studio 代码自动导入完成)引起的。

你只需要改变

import { EventEmitter } from 'events';

import { EventEmitter } from '@angular/core';

以及关于错误的解释。 Angular 将事件映射为可观察对象,因此它必须订阅子组件的事件并将它们发送到适当的组件。

在寻找原始包时,我在以下位置找到了它的源代码:https://github.com/Gozala/events/blob/master/events.js

它只是失败了,因为在您创建的 EventEmitter 中没有方法 .suscribe()

关于angular - instance[output.propName].subscribe 不是由 polyfill 生成的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48596826/

相关文章:

angular - 如何用 Angular 跳过过时的响应?

javascript - NestJS:在自定义模块中使用 forRoot/forChild - 竞争条件?

angularjs - 尝试测试工厂时遇到注入(inject)问题

reactjs - 在 jasmine 助手中调用 Enzyme.configure

javascript - Karma --auto-watch 不再有效

angular - 如何重置 Angular Reactive Form

Angular 2 项目结构

reactjs - typescript 如何混合动态([key : type]: type) and static typing for an interface

Angular 模块从 es6 到 commonJs 结果

twitter-bootstrap - Angular 2 中的 Bootstrap 导航菜单