簡體   English   中英

ES6-擴展時未定義類方法-babel問題

[英]ES6 - Class Methods not defined when extending - babel issue

我正在擴展一個類,並為其添加一些屬性和方法:

class Cmp extends ConsentString {
  constructor(result = null) {
    super(result);
    this.setCmpId(52);
  }
  loaded() {
    return this.cmpLoaded;
  }
}

然后,我正在創建一個函數,該函數返回創建類的新實例的承諾:

export default function initCmp(loaderData) {
  return new Promise((resolve, reject) => {
    const cmp = new Cmp();
    console.log('testing', cmp.loaded()); // undefined!
    if (!cmp) reject();
    console.log('CMP: ', cmp); // this logs as you would expect apart from there are non of the methods with which I extend.
    resolve(cmp);
  });

我可以訪問原始類中的方法和屬性,以及擴展中的任何屬性,但不能訪問任何方法-它們都是未定義的。

我嘗試了很多事情,例如bind(this)並將方法名稱添加到構造函數中,似乎沒有任何效果。

我還感覺到此錯誤是作為添加的Babel Transform Runtime的一部分引入的,以便能夠使用async / await。

編輯:ConsentString類在這里https://github.com/InteractiveAdvertisingBureau/Consent-String-SDK-JS/blob/master/src/consent-string.js

更新:我試圖在不擴展類的情況下運行流,而是創建一個新的類,一切正常-因此問題必須與擴展有關,但是我不確定那是什么,這仍然是一個問題。

更新2:當我從babel中刪除兩個插件時: @babel/plugin-transform-runtimetransform-async-to-generator都可以正常工作。

請幫忙!

我已經(由於與@JamesLeClair聊天)包含了babel-polyfill而不是解決了問題的運行時。

這不是完美的解決方案,因為我正在圖書館工作,但現在可以讓一切保持運轉!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM