ES6 + Babel:获取TypeError:actions.helloOne()不是函数
问题描述:
我开始用webpack和babel构建一个React应用程序(弹出create-react-app)。不过,我无法使用实用程序功能导入外部.js文件。ES6 + Babel:获取TypeError:actions.helloOne()不是函数
每当我尝试拨打actions.helloOne()
时,出现以下错误:TypeError: actions.helloOne is not a function
。
我对JS仍然很陌生,所以我可能会错过一些明显的东西。
helloActions.js
export function helloOne() {
console.log('one')
}
export function helloTwo() {
console.log('two')
}
actions.js
import { helloOne, helloTwo } from './helloActions'
export {
helloOne,
helloTwo
}
service.js
import * as actions from './actions'
actions.helloOne(); <- undefined
的package.json
"babel-core": "6.22.1",
"babel-eslint": "7.1.1",
"babel-jest": "18.0.0",
"babel-loader": "6.2.10",
"babel-preset-react-app": "^2.2.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-stage-2": "6.5.0",
"babel-plugin-transform-decorators-legacy":"1.3.4",
"babel-plugin-transform-class-properties": "6.11.5",
...
"babel": {
"presets": [
"react-app",
"es2015",
"stage-2"
],
"plugins": ["transform-decorators-legacy","transform-class-properties"]
}
答
我设法解决我的问题,该代码是正确的,因为提到安德鲁·李。
我在导入行中的某个文件的路径中出现了一个小错误(基本上错过了导致babel在不发生错误的情况下做怪异事情的.js)。
'import * as actions'?为什么不“输入动作”? – evolutionxbox
这很奇怪。它对我来说工作得很好。 – Li357
@AndrewLi对,我看起来100%正确,所有其他的例子也是一样的。它可能是巴贝尔或其他东西的坏版本? –