【问题标题】:Type Error on Transpiling Babel class.default is not a constructorTranspiling Babel 类的类型错误。默认不是构造函数
【发布时间】:2016-10-21 15:07:23
【问题描述】:
'use strict';
/**
* PortalHTML.js
*/

import SingleContextHTML from './SingleContextHTML';
import MultiContextHTML from './MultiContextHTML';

export default class PortalHTML{
    constructor (type) {
          switch(type) {
        case "1":
          this.portalStrategy = new SingleContextHTML();
          break;
        default:
          this.portalStrategy = new MultiContextHTML();
          break;
           }
    }

    render (...args) {
        this.portalStrategy.renderHTML(args);
    }

    _strategyPeak(){
        return this.portalStrategy.constructor.name;
    }
}

/**
* SingleContextHTML.js
*/
'use strict';

import PortalHTMLStrategy from './PortalHTMLStrategy';

export default class SingleContextHTML extends PortalHTMLStrategy {
    constructor(){
            super()
    }
    renderHTML(args){}
}

/**
* Multi.js (same as single) ^^ above
*/

/**
* PortalHTMLStrategy.js
*/
'use strict';

export default class PortalHTMLStrategy{
    constructor(){}

    renderHTML(args){}
}

/**
* Tester Mocha and Chai
*/

'use strict';

import PortalHTML from "./PortalHTML";
import chai from 'chai';

let basicPortalHTMLTest = () => {
    let singleContextHTML = new PortalHTML("1");
    let multiContextHTML = new PortalHTML("multi");
    describe('PortalHTML: Initialization of PortalHTML', () => {
        it('Should be an instance of class SingleContextHTML', (done) => {
            chai.expect(singleContextHTML._strategyPeak()).to.equal('SingleContextHTML');
            done();
        });

        it('Should be an instance of MultiContextHTML', (done) => {
            chai.expect(singleContextHTML._strategyPeak()).to.equal('MultiContextHTML');
            done();
        });
    });
};

basicPortalHTMLTest();

我正在尝试测试策略模式的实现,但是在我使用 mocha 运行测试脚本后遇到了一个错误,指出以下内容。

var singleContextHTML = new _PortalHTML2.default("1");
                                      ^                               
TypeError: _PortalHTML2.default is not a constructor

我正在使用 node6 和以下 babel 包:

    "babel-cli": "^6.14.0",
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-2": "^6.11.0"

我已经尝试调试了大约半天。如果有人立即发现我做错了什么,我会非常感激不尽。

【问题讨论】:

  • import PortalHTML from "PortalHTML"; 似乎包含错误的模块标识符。
  • 你的意思是from "./ProtalHTML" 吗?
  • 是的,对于这个玩具示例,在我的实际代码中,目录结构是正确的。我更新以防止混淆。对此感到抱歉

标签: node.js ecmascript-6 mocha.js babeljs


【解决方案1】:

我使用了不正确的 babel 预设。切换到 es2016 或更高版本修复了此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2011-03-19
    • 2021-08-20
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多