ค้นหาเว็บไซต์

angular - instance[output.propName].subscribe ไม่ใช่ฟังก์ชัน ซึ่งเกิดจาก polyfill


ฉันกำลังเจอปัญหาแปลกๆ ฉันได้ค้นคว้าเกี่ยวกับข้อผิดพลาดนี้แล้ว และโดยทั่วไปดูเหมือนว่าจะเกี่ยวข้องกับการใช้ @Output หรือ EventEmitter แต่ในกรณีนี้ไม่ใช่

ฉันได้รับข้อผิดพลาดดังต่อไปนี้ในแอปพลิเคชันของฉันหากฉันมีโพลีฟิลล์เฉพาะอยู่

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)

โพลีฟิลล์มีดังนี้:

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 ซึ่งไม่ทำให้เกิดปัญหา

คำตอบ #1

ฉันเพิ่งเจอปัญหานี้และเมื่อเช็กโค้ดที่ฉันเขียนก็พบว่ามันเกิดจากการนำเข้าคลาส EventEmitter ไม่ถูกต้อง (ซึ่งเกิดจากการนำเข้าของ Visual Studio Code อัตโนมัติ)

คุณแค่ต้องเปลี่ยน

import { EventEmitter } from 'events';

ถึง

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

และเกี่ยวกับคำอธิบายข้อผิดพลาด Angular จะแมปเหตุการณ์เป็น observables ดังนั้นมันต้องสมัครรับเหตุการณ์ของคอมโพเนนต์ลูกและส่งไปยังคอมโพเนนต์ที่เหมาะสม

กำลังมองหาชุดโปรแกรมต้นฉบับ ฉันพบโค้ดต้นฉบับของมันที่: https://github.com/Gozala/events/blob/master/events.js

และมันล้มเหลวอย่างง่ายดายเพราะไม่มีเมธอด .suscribe() ใน EventEmitter ที่คุณสร้างขึ้น

บทความที่เกี่ยวข้อง