Javascript es6在类中导入类
在index.js中,我有Javascript es6在类中导入类,javascript,webpack,ecmascript-6,es6-modules,es6-class,Javascript,Webpack,Ecmascript 6,Es6 Modules,Es6 Class,在index.js中,我有 import PageLoader from './pageLoader'; $(function() { const pageLoader = new PageLoader(); }); 和pageloader.js class PageLoader{ constructor(){ { this.customer = null; this.data = []; this.init(); }
import PageLoader from './pageLoader';
$(function() {
const pageLoader = new PageLoader();
});
和pageloader.js
class PageLoader{
constructor(){
{
this.customer = null;
this.data = [];
this.init();
}
}
init() { }
}
module.exports = PageLoader;
一切正常。但是如果我从页面加载器导入一个类
import Customer from './customer';
class PageLoader{
constructor(){
{
this.customer = null;
this.data = [];
this.init();
}
}
init() { }
}
module.exports = PageLoader;
和customer.js
class Customer{
constructor(){
this.data = [];
this.init();
}
init() {
}
}
module.exports = Customer;
我收到
./src/index.js 10:23-33“导出‘默认’(导入为默认)”中的警告
在“./PageLoader”中找不到“PageLoader”)
module.exports
语法是from(在NodeJs中大量使用,对应的是require
而不是import)。如果要使用import
,需要使用export
子句,它是from
您还可以进行命名导出
export { PageLoader };
然后
import { PageLoader } from './pageLoader';
module.exports
语法是from(在NodeJs中大量使用,对应的是require
而不是import)。如果要使用import
,需要使用export
子句,它是from
您还可以进行命名导出
export { PageLoader };
然后
import { PageLoader } from './pageLoader';
为什么要导入一个类而从不使用它?我使用它只是不想用额外的代码来混淆示例OK,但这只是一个警告-代码实际工作吗?不,它不工作。你混合了commonjs和es6模块。modules.export来自commonjs(nodejs),import来自es6模块(es6 js)为什么你要导入一个类而从不使用它?我使用它只是不想用额外的代码弄乱示例OK,但这只是一个警告-代码实际工作吗?不,它不工作。你混合了commonjs和es6模块。modules.export来自commonjs(nodejs),import来自es6模块(es6 js)