【问题标题】:Importing es6 module, some named exports are undefined导入 es6 模块,一些命名导出未定义
【发布时间】:2016-07-03 22:30:08
【问题描述】:

我正在notification_actions.js 中编写代码,例如:

# notification_actions.js
export const NOTIFICATIONS_RECEIVED = 'NOTIFICATIONS_RECEIVED';
export const notificationsReceived = (notifications, unreadCount) => ({
  type: NOTIFICATIONS_RECEIVED,
  notifications,
  unreadCount,
});

(见whole file

然后将其捆绑到 all_actions.js 中,例如:

# all_actions.js
export * from 'navigation_actions';
export * from 'filter_type_actions';
export * from 'notification_actions';

最后在notifications_model.js 中使用它作为:

import {
  notificationsReceived,
} from './all_actions.js';

...

const handleData = (dispatch) => ({ notifications, unreadCount }) => {
  dispatch(notificationsReceived(notifications, unreadCount));
};

但我得到TypeError: notificationsReceived is undefined

不确定如何进一步调试。

我的 webpack.config.js:here

我的package.json部门:

"autobind-decorator": "^1.3.3",
"autoprefixer": "^6.3.6",
"babel-cli": "^6.10.1",
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"babel-plugin-export-default-module-exports": "^0.0.4",
"babel-plugin-lodash": "^3.2.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-es2015-modules-commonjs-simple": "^6.7.4",
"babel-polyfill": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"classnames": "^2.2.5",
"colorguard": "^1.2.0",
"composer": "^0.3.0",
"css-loader": "^0.23.1",
"eslint": "^2.11.1",
"eslint-config-airbnb": "^9.0.1",
"eslint-import-resolver-webpack": "^0.3.0",
"eslint-loader": "^1.3.0",
"eslint-plugin-import": "^1.8.1",
"eslint-plugin-jsx-a11y": "^1.3.0",
"eslint-plugin-react": "^5.1.1",
"formsy-react": "^0.18.0",
"glob": "^7.0.3",
"image-webpack-loader": "^1.8.0",
"lodash": "^4.13.1",
"lodash-webpack-plugin": "^0.9.1",
"moment": "^2.13.0",
"node-sass": "^3.7.0",
"postcss": "^5.0.21",
"postcss-filter-stream": "0.0.6",
"postcss-loader": "^0.9.1",
"radium": "^0.17.1",
"react": "^15.1.0",
"react-bootstrap-sweetalert": "^1.1.11",
"react-click-outside": "^2.1.0",
"react-datepicker": "^0.27.0",
"react-dom": "^15.1.0",
"react-mixin": "^2.0.2",
"react-multidecorator": "^0.1.0",
"react-onclick-outside": "^0.1.0",
"react-redux": "^4.4.5",
"react-select": "^1.0.0-beta13",
"react-toggle-display": "^2.0.2",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0",
"resolve-url-loader": "^1.4.3",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"stylelint": "^6.5.1",
"underscore": "^1.8.3",
"url-loader": "^0.5.7",
"webpack": "^1.13.1",
"webpack-plugin-manifest": "^1.0.1"

【问题讨论】:

  • action_creators.js 定义在哪里?

标签: javascript ecmascript-6 webpack redux babeljs


【解决方案1】:

问题是循环依赖,特别是在脚本中使用export defaultimport DependencyName from 'dependency'

代替:

// action.js
import SomeModal from './some_modal.js';
export const doAction = () => console.log(SomeModal.modalId);
export default module.exports;

// some_modal.js
import { doAction } from './action.js';
export const close = () => doAction();
export default module.exports;

这行得通:

// action.js
import { modalId as SomeModalId } from './some_modal.js';
export const doAction = () => console.log(SomeModalId);
export default module.exports;

// some_modal.js
import { doAction } from './action.js';
export const close = () => doAction();
export default module.exports;

注意action.js中的import { modalId as SomeModalId }

【讨论】:

    【解决方案2】:

    因为你在 all_actions.js 中捆绑了动作

       # all_actions.js
        export * from './navigation_actions';
        export * from './filter_type_actions';
        export * from './notification_actions';
    

    您应该从 all_actions.js

    导入
    import { notificationsReceived } from './your-path/all_actions';
    

    或直接来自 notification_actions.js

    import { notificationsReceived } from './your-path/notification_actions';
    

    import * as actions from './your-path/all_actions';
    ///then
    actions.notificationsReceived;
    ///or
    const {notificationsReceived} = actions;
    

    【讨论】:

    • 抱歉,需要编辑我的问题。我正在从 all_actions 导入
    • navigation_actions.js 与 all_actions.js 在同一文件夹中吗?
    • 所有文件都在解析中。
    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 2020-05-02
    • 2015-07-21
    • 2017-11-19
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多