以不同的名称导入ES6类(Importing ES6 Class under a different name)

编程入门 行业动态 更新时间:2024-10-11 17:25:46
以不同的名称导入ES6类(Importing ES6 Class under a different name)

使用gulp , browserify和babelify ,导出/导入类工作正常,直到我尝试使用不同的名称导入相同的类:

// Acme/DefaultInit.js export default class DefaultInit { constructor() { console.log('hello'); } } // App/Init.js import * as B from "../Acme/DefaultInit"; class Init extends B.DefaultInit { constructor() { super(); console.log('how are you?'); } }

所以当我运行gulp构建的脚本时,错误是:

TypeError: Super expression must either be null or a function, not undefined

来自babel生成的代码

if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); }

我在那里做错了什么? 请注意,没有jslint错误

Using gulp, browserify & babelify, exporting/import classes worked fine until I tried to import the same class under a different name:

// Acme/DefaultInit.js export default class DefaultInit { constructor() { console.log('hello'); } } // App/Init.js import * as B from "../Acme/DefaultInit"; class Init extends B.DefaultInit { constructor() { super(); console.log('how are you?'); } }

So when I run the gulp built script, the error is:

TypeError: Super expression must either be null or a function, not undefined

coming from the babel generated code

if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); }

What I'm doing wrong there ? Note that are no jslint errors

最满意答案

您将DefaultInit导出为默认值,因此它实际上可以从B.default获得, B.default不是B.DefaultInit 。

如果你想能够做B.DefaultInit从class DefaultInit之前删除default ,或者替换import * as B来import DefaultInit 。

You're exporting DefaultInit as a default so it'll actually be available from B.default not B.DefaultInit.

If you want to be able to do B.DefaultInit drop the default from before class DefaultInit or alternatively replace import * as B to import DefaultInit.

更多推荐

本文发布于:2023-07-30 15:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   Class   Importing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!