简体   繁体   English

ES6和Webpack:导入无效

[英]ES6 & Webpack: import not working

I'm currently learning ES6 & Webpack, and I've have a particular set of files that the import statement just won't work on. 我目前正在学习ES6和Webpack,而且我有一组特定的文件, import语句无法正常工作。 Here's the problem: 这是问题所在:

Sagas.js Sagas.js

import { deleteMe } from './DeleteMe';
import * as constants from '../Constants';

debugger;

DeleteMe.js DeleteMe.js

export const deleteMe = "Yep, it's loading";
console.log(deleteMe);

In the above file, deleteMe is not accessible from debugger (with Chrome Inspector). 在上面的文件中,无法从调试器访问deleteMe (使用Chrome Inspector)。 constants is. constants是。

And Constants.js 和Constants.js

export const SET_COMMENTS = 'SET_COMMENTS';

Before debugger halts (in Chrome inspector), the "Yep, it's loading" does fire in the console, so the file itself is coming through. 在调试器停止之前(在Chrome检查器中),“Yep,它正在加载” 在控制台触发,因此文件本身就会通过。

I've tried: 我试过了:

  • import * as deleteMeStuff from ./DeleteMe to see if deleteMeStuff would populate. import * as deleteMeStuff from ./DeleteMe ,看看是否会填充deleteMeStuff It doesn't 它没有
  • Restarting webpack-dev-server. 重新启动webpack-dev-server。 No dice, no errors on compile. 没有骰子,编译时没有错误。 Throwing a debugger in ./DeleteMe confirms changes are coming through. 在./DeleteMe中输入debugger确认更改即将到来。
  • const deleteMe = "Yep, it's working"; export default deleteMe const deleteMe = "Yep, it's working"; export default deleteMe & import deleteMe from './DeleteMe' --> still no joy const deleteMe = "Yep, it's working"; export default deleteMeimport deleteMe from './DeleteMe' - >仍然没有乐趣

At the debugger, I've just noticed _DeleteMe returns {deleteMe: "Yep, it's loading", __esModule: true} 在调试器中,我刚刚注意到_DeleteMe返回{deleteMe: "Yep, it's loading", __esModule: true}

Any idea what's going on here & how to fix it? 知道这里发生了什么以及如何解决它? I'm thoroughly confused. 我很困惑。 There are a ton of other import files working successfully in other files across my app. 在我的应用程序中,有大量其他导入文件在其他文件中成功运行。

You can't use default and const in same line like 你不能在同一行中使用default和const

export default const deleteMe = "Yep, it's working"

You have to break it like: 你必须打破它:

export const deleteMe = "Yep, it's loading";
export default deleteMe;

and to import it you can do any one of following: 并导入它您可以执行以下任一操作:

import deleteMe  from './DeleteMe';

or 要么

import  { deleteMe }  from './DeleteMe';

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

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