繁体   English   中英

Javascript es6 + Webpack引用导入

[英]Javascript es6 + Webpack referencing imports

我试图弄清楚为什么我可以从一个文件中引用导入,但不能从另一个文件中引用(我认为这与javascript的工作方式有关,并且在定义变量之前我无法引用它。)那么我希望你们中的一个能给我一些解决方法的参考。)我正在使用webpack和babel构建一个电子应用程序。

我的webpack.config.js看起来像这样:

var path = require('path');
var webpack = require('webpack');

module.exports = {
 entry: {
  app: ['./app/src/app.js', './app/src/test.js']
 },
 output: {
  path: __dirname,
  filename: './app/lib/bundle.js'
 },

 module: {
  loaders: [
   {
    test: /.js?$/,
    loader: 'babel-loader',
    exclude: /node_modules/,
    query: {
     presets: ['env']
    }
   }
  ]
 },
};

还有我的app.js

import { test, foo } from './app';

class app {
 constructor() {
  console.log("hello from app.js");
 }
}

// let a = new test(); // gives error (shown below)
// foo(); // same exact error but referencing function instead of constructor

export { app };

和test.js

import { app } from './app';

class test {
 constructor() {
  console.log("hello from test.js");
 }
}

let foo = () => console.log("foo");

let b = new test(); // works as expected

export { test, foo };

错误如下:

Uncaught TypeError: _app.test is not a constructor
    at Object.<anonymous> (bundle.js:88)
    at __webpack_require__ (bundle.js:20)
    at Object.defineProperty.value (bundle.js:97)
    at __webpack_require__ (bundle.js:20)
    at Object.defineProperty.value (bundle.js:63)
    at bundle.js:66

因此,我可以在测试内从应用程序调用类,但是在其他方法上不能做同样的事情吗? 为什么?

感谢您的提前回复

这是一个循环依赖性问题。 您的app导入test ,该test又导入app

谢谢评论者卢克和李357。 我为此烦恼不已,并且对文件夹名称与文件名称感到困惑。

问题是我从同一位置导入文件,并导致循环依赖项错误。

解决了。

暂无
暂无

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

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