我使用./node_modules/.bin/webpack -d
将代码打包。除了这个class fields proposal
,我没有将ES6编译为ES5。
它会出现以下错误:
未捕获的类型错误:this.fetchTestExecutions不是函数
以下是代码:
import React from 'react'
import Config from 'Config'
class HomePage extends React.Component {
state = {
executions: this.fetchTestExecutions()
}
fetchTestExecutions = () => {
const host = Config.host
return fetch(`${host}/execution/index`)
.then(response => response.json())
.then(json => {
this.setState({executions: json})
})
}
render() {
return(
<div>
{ this.state.executions.map(x => <div>x.from</div>) }
</div>
)
}
}
export default HomePage
这是webpack.config.js文件的内容:
var webpack = require('webpack')
module.exports = {
entry: './src/App.jsx',
output: {
filename: './../public/bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
query: {
plugins: ['transform-class-properties'],
presets: ['react'],
}
}
]
},
externals: {
'Config': JSON.stringify({host: "http://127.0.0.1:3000"})
}
}
有什么问题吗?
感谢您的时间!
randomString =()=>“a string”
和state = {executions:this.randomString()}
,它仍然会给出相同的错误。state
是类变量吗?this.randomString()
是实例方法吗? - mCYstate
是一个对象文档链接。我不会使用方法(例如randomString
)来设置它的初始属性。如果您在组件内定义了this.randomString()
,则它是一个实例方法(允许您从组件内的任何位置调用this.randomString()
)。 - Hemerson Carlin