Unexpected “Uncaught TypeError: XXX is not a constructor” errors with Babel and ES6

前端 未结 6 486
無奈伤痛
無奈伤痛 2020-12-29 19:04

I am giving a try to Webpack, and am giving a try to the instructions in this tutorial, give or take a few custom things.

This is simple code, really, but I\'m quite

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 19:51

    I realize that you already have an answer. However I had a similar issue to which I found an answer. Starting my own question and answering it seems weird. So I'm just going to leave this here.

    I had the same error as you got. However, I managed to solve it by changing my

    export default {Class}
    

    to

    export default Class
    

    I don't know why I wrapped the Class in an object but I remember having seen it somewhere so I just started using it.

    So instead of the default returning a Class it returned an object like this {Class: Class}. This is completely valid yet it will break webpack+babel.

    EDIT: I've since come to know why this probably breaks babel+webpack. The export default is meant to only have 1 export. A javascript-object can contain many properties. Which means it can have more than 1 export. (See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export).

    For multiple exports use: export {definition1, definition2}.

    Use-case: I've used this in a situation where I've created a library which exported different types of an editor (while the underlying code was the same, the appearance of the editor changes depending on which export you use).

提交回复
热议问题