以其他名称导入 ES6 类

Importing ES6 Class under a different name

本文关键字:ES6 导入 其他      更新时间:2023-09-26

使用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 错误

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

.

如果您希望能够这样做B.DefaultInit请从class DefaultInit之前删除default,或者将import * as B替换为import DefaultInit