[英]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.