[英]How do I import a class to extend with ES6 and Webpack?
I'm new to webpack and am trying to require or import a class to extend in another file, and continually end up with the class being undefined. 我是webpack的新手,正在尝试要求或导入一个类以在另一个文件中扩展,并最终导致该类未定义。 Here's the file setup.
这是文件设置。
app.js app.js
require('./stuff.js');
stuff.js stuff.js
require('./Subclass.js');
Subclass.js Subclass.js
require('./Superclass.js');
class Subclass extends Superclass {
}
Superclass.js 超类.js
class Superclass {
}
And compiling with webpack via webpack app.js bundle.js
并通过
webpack app.js bundle.js
与webpack进行编译
With this I end up with Superclass is not defined
when including bundle.js in the browser. 这样我最终在浏览器中包含bundle.js时
Superclass is not defined
。
I have also tried using import with exports, doing 我也尝试过将import与export结合使用,
Subclass.js 子类
import Superclass from './Superclass';
// I have also tried
// import {Superclass} from './Superclass';
class Subclass extends Superclass {
}
Superclass.js 超类.js
export default class Superclass {
}
But that results in Superclass being undefined when trying to extend, causing the error Super expression must either be null or a function, not object
但这导致尝试扩展时未定义Superclass,从而导致错误
Super expression must either be null or a function, not object
This is my webpack.config.js 这是我的webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
},
stats: {
colors: true
},
devtool: 'source-map'
};
Your initial stab at it was wrong because: 您最初的尝试是错误的,因为:
Superclass
correctly Superclass
Subclass
or Superclass
Subclass
或Superclass
Your second attempt is closer as your importing Superclass
correctly but again you fail export Subclass
- to summarise, this should fix your code 您的第二次尝试接近了,因为您正确地导入了
Superclass
,但是再次失败了导出Subclass
-概括地说,这应该可以修复您的代码
Subclass.js 子类
import Superclass from './Superclass';
export default class Subclass extends Superclass {
...
}
Superclass.js 超类.js
export default class Superclass {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.