我正在尝试使用新的Rails应用程序上的Webpacker来熟悉它,我无法编译我的javascript代码。
以下是代码:
import React, { Component, PropTypes } from 'react'
import DayPicker, { DateUtils } from 'react-day-picker'
import 'react-day-picker/lib/style.css'
export default class Example extends Component {
constructor(props) {
super(props)
this.state = {
selectedDays: []
}
}
handleDayClick = (day, selected) => {
console.log(day)
}
render() {
return (
<p>Test</p>
)
}
}
20:05:21 hot.1 | ERROR in ./app/javascript/packs/components/example/example.jsx
20:05:21 hot.1 | Module build failed: SyntaxError: Missing class properties transform.
20:05:21 hot.1 |
20:05:21 hot.1 | 11 | }
20:05:21 hot.1 | 12 |
20:05:21 hot.1 | > 13 | handleDayClick = (day, selected) => {
20:05:21 hot.1 | | ^
这是一个全新的Rails应用程序,使用来自github的最新Webpacker gem。
我的.babelrc
:
{
"presets": ["env", "es2015", "react", "stage-2"]
}
我的devDependencies是:
"devDependencies": {
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-2": "^6.22.0",
"webpack-dev-server": "^2.4.2"
}
有什么想法吗?
答案 0 :(得分:1)
要修复,我必须将预设添加到各个加载程序文件中,具体而言。
// config/webpack/loaders/babel.js
module.exports = {
test: /\.js(\.erb)?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
'es2015',
'stage-2'
]
}
}
// config/webpack/loaders/react.js
module.exports = {
test: /\.(js|jsx)?(\.erb)?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
'es2015',
'react',
'stage-2'
]
}
}
现在我可以使用漂亮的速记箭头类函数了。
答案 1 :(得分:-1)
你的评论最有启发性:这是一个类中的方法声明;应该没有等号或胖箭。这应该是它的样子:
handleDayClick (day, selected) {
console.log(day)
}
了解constructor
和render
方法的外观;这就是反应组件中所有方法的外观。
d&#39;!哦