简体   繁体   English

在将webpack与babel结合使用时,为什么使用命名导出导入默认导出会导致错误?

[英]Why does importing a default export with a named export cause an error when using webpack with babel?

I have 2 constructors, SignUp and GoogleSignIn. 我有2个构造函数,SignUp和GoogleSignIn。 Structurally, they look like this: 从结构上看,它们如下所示:

import SignUp, {gapi_promise} from "./SignUp";
/**
*
* @param element
* @extends SignUp
* @constructor
*/
function GoogleSignIn(element){

    SignUp.call(this);
}

GoogleSignIn.prototype = Object.create(SignUp.prototype);

export default GoogleSignIn;

and

function SignUp(){
    //Some constructor code
}

export let gapi_promise = (function(){

    return new Promise((resolve, reject) => {
        //Do some promise stuff with the google api
    });
}());

export default SignUp;

I've been using webpack with the babel loader to bundle these and other assets together, but when I load my page I get the error: 我一直在使用webpack和babel加载器将这些和其他资产捆绑在一起,但是在加载页面时出现错误:

GoogleSignIn.js?1051**:21 Uncaught TypeError: Cannot read property 'prototype' of undefined(…)

Basically, the value of SignUp is undefined. 基本上,SignUp的值是未定义的。 Am I importing or exporting values incorrectly? 我是否错误地导入或导出值?

Specifically, the line that fails is this one: 具体来说,失败的行是这一行:

GoogleSignIn.prototype = Object.create(SignUp.prototype);

I can provide additional details if necessary. 如果需要,我可以提供其他详细信息。 Thank you very much! 非常感谢你!

It maybe because you are using Babel 6, which compiles modules according to ES6 specifications. 可能是因为您使用的是Babel 6,它根据ES6规范编译模块。 If it is this case, you have two solutions: 如果是这种情况,您有两种解决方案:

1.Change GoogleSignIn.prototype = Object.create(SignUp.prototype); 1. GoogleSignIn.prototype = Object.create(SignUp.prototype); to GoogleSignIn.prototype = Object.create(SignUp.default.prototype); GoogleSignIn.prototype = Object.create(SignUp.default.prototype);

2.Use this plugin( https://www.npmjs.com/package/babel-plugin-add-module-exports ) to get the old export behavior back. 2.使用此插件( https://www.npmjs.com/package/babel-plugin-add-module-exports )恢复旧的导出行为。

You can refer to Babel 6 changes how it exports default 您可以参考Babel 6的更改如何导出默认值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么命名导出导致“未找到导出默认值”..错误 - Why does named export lead to “Export default was not found”..error 导入默认导出和命名导出是否会将模块加载两次? - Does importing the default export along with a named export load the module twice? 使导出默认值与Babel,webpack和Node.js一起使用 - Making export default work with Babel, webpack and Node.js 使用 importmap 使用 jquery 会出现错误“请求的模块 'jquery' 不提供名为 'default' 的导出” - Consuming jquery using importmap gives error "The requested module 'jquery' does not provide an export named 'default'" 动态导入使用Webpack命名导出 - Dynamic import named export using Webpack 将“导出默认值”更改为命名导出 - Change 'export default' to named export webpack 导出默认有效,而导出对象无效 - webpack export default works while export object does not 浏览器抛出导出/导入关键字的错误,甚至安装了webpack和babel - browser throw error of export/import keyword even webpack and babel is installed 使用命名和默认导出的汇总库导入文件不起作用 - Rollup library importing file with named and default export not working 为什么 fullcalendar 会给出它不提供名为“默认”的导出的错误? - Why does fullcalendar give the error that it does not provide an export called 'default'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM