为什么我的函数被导入为值' 0'在ES6?

时间:2016-09-27 15:36:45

标签: reactjs ecmascript-6 redux react-redux

我正在使用redux和ES6在react.js中工作。我有一个动作文件导出一个像这样的函数:

MonthlyRevenueActions.js

export function fetchMonthlyRevenue(name, startDate, endDate) {
    return function(dispatch) {
        dispatch(console.log("name": " + name + ", start: " + startDate + ", end: " + endDate));
    }
}

然后在我的容器代码中,我有一个import语句并尝试使用该函数,但我可以在Chrome DevTools中看到变量的值为0:

MonthlyDisplayContainer.js:
import {otherFunc, fetchMonthlyRevenue, otherFunctionTwo} from 'actions/monthly/MonthlyRevenueActions'

const mapStateToProps = (state, ownProps) => {

    //...  other code that's being executed

    fetchMonthlyRevenue(name, startDate, endDate);
}

Chrome DevTools snapshot

我可以看到namestartDateendDate的值,但fetchMonthlyRevenue()函数只是0。

我希望在涉及范围时我错过了一些东西(我是ES6的新手),但是对于我的生活,我们无法弄清楚如何执行这个功能。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

Chrome可能会提升您导入的已转换来源。你使用的是源图吗?也许它们没有被正确地创建或加载。

Babel对导入执行以下操作...

<强>在...

import { a } from 'a'
a()

<强>缺货...

'use strict';

var _a = require('a');

(0, _a.a)();

Check it out on the Babel REPL

(这取决于导入的类型,例如默认,命名。)

注意如何在最后一行更改导入值的函数调用:

(0, _a.a)();