简体   繁体   English

ES6 + BabelJS + Webpack导出类

[英]ES6 + BabelJS + Webpack to export classes

I was learning about babel and the new stuff ES6 that looks amazing, but I'm struggling with import classes from different 'js'. 我正在学习babel和看起来很棒的新东西ES6,但是我正在努力处理来自不同“ js”的导入类。

I have the dummy.js: 我有dummy.js:

export default class Test {
    construct(){
        this.name = "name";
    }

    getName(){
        return this.name;
    }
}

And i have my index.js (entry point on webpack config): 我有我的index.js(关于webpack配置的入口点):

import Test from './dummy.js';
var ab = new Test();

console.log(ab);
//out: Test{}
console.log(ab.getName());
//out: undefined

I spent some hours googling but I've not been able to find anything that works. 我花了几个小时进行谷歌搜索,但找不到任何有效的方法。 I tried to use 'module.exports' or even export a function to return new instance of 'Test', but it doesn't work too. 我试图使用'module.exports'或什至导出一个函数以返回'Test'的新实例,但是它也不起作用。

The only way that works was utilizing prototype, it seems to do the work, but before coding everything utilizing prototype i want to be sure that class doesn't work. 唯一可行的方法是利用原型,它似乎可以完成工作,但是在利用原型编码​​一切之前,我要确保该类不起作用。

I even tried make 'dummy.js' an entry of webpack config, but it would implicit a static class and i don't want it. 我什至尝试将'dummy.js'用作webpack配置的条目,但是它将隐含一个静态类,我不想要它。

Then anyone know how to bypass this ? 那么有人知道如何绕过这个吗? Or I'm forgetting something ? 还是我忘记了什么?

The name of constructor in ES2015+ is constructor not construct . ES2015 +中的constructor construct的名称是constructor not construct

References: 参考文献:

您的construct函数应命名为constructor

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM