【问题标题】:ES6 Inheritance From Imported Module in Webpack从 Webpack 中导入的模块继承 ES6
【发布时间】:2017-05-21 22:56:31
【问题描述】:

所以我遇到了在 Webpack 中扩展超类的子类的问题。

我的超类:core/Main.js

class Main {
    constructor () {
        console.log('Main Class Initialized');
    }
}

module.exports = Main;

子类:app/Launch.js

var Main = require('core/Main.js');
class Launch extends Main {

    constructor () {
        console.log('Before Super')
        super();
        console.log('Launch Class Initialized')
    }
}

如果我在 app/Launch.js 文件中 console.log(Main) 它会记录 Main 并且“Before Super”也会被记录,但调用 super() 会导致它中断,我不知道为什么。

How to achieve inheritance in ES6 with “webpack module bundler”? 没有帮助。我尝试将module.exports 换成export class Main {}require('core/Main.js') 换成import {Main} from 'core/Main.js',但是没有用。使用 webpack 1.14.0。

【问题讨论】:

  • 继承与 webpack 无关。看来您的问题实际上类似于“使用 webpack 时如何正确导入模块”?

标签: javascript inheritance webpack es6-class


【解决方案1】:

知道了。我将Main.js 复制到app/ 并要求这样做。现在工作正常,但不知道为什么会有所作为。

【讨论】:

  • 我不知道你的项目结构是什么,但是如果 app 和 core 文件夹处于同一级别,那么你在 Launch.js 文件中使用的需要 Main.js 的路径是错误的,它应该是'../core/Main.js'
  • 所以结构是src/app和src/core。我将配置中的上下文设置为 src 以便我可以调用 require('app/file') require('core/file') - 该上下文适用于我项目中的其他所有内容,但由于某种原因不在此src/core 的情况。
猜你喜欢
  • 2018-11-12
  • 2016-06-24
  • 2016-11-26
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2016-04-09
  • 2020-02-14
相关资源
最近更新 更多