【发布时间】:2017-12-22 02:38:19
【问题描述】:
我正在使用一个名为 Papa Parse 的库将 csv 文件解析为 json。解析器工作正常,但我无法在 Papa 解析的配置对象的“完整”部分中指定的回调函数中调用我的操作创建者。我相信这是一个范围问题,但我一直无法解决。下面感兴趣的主要部分是为什么我不能调用“printFile”函数中的任何一个函数,最重要的是“addRoster”函数。请让我知道您有任何建议或想法。谢谢!
编辑:忽略这是 sn-p 形式。这里的内容不可运行,我只是无法让格式化工作。
class Roster extends Component {
super(props) {
this.printFile = this.printFile.bind(this);
this.testMethod = this.testMethod.bind(this);
}
componentWillMount() {
this.props.addRoster('test')
}
printFile(results, file) {
this.props.addRoster(file);
this.props.testMethod;
}
testMethod(test) {
console.log("Winner")
}
fileDrop(file) {
var config = {
delimiter: "", // auto-detect
newline: "", // auto-detect
quoteChar: '"',
header: true,
dynamicTyping: false,
preview: 0,
encoding: "",
worker: false,
comments: false,
step: undefined,
complete: this.printFile,
error: undefined,
download: false,
skipEmptyLines: false,
chunk: undefined,
fastMode: undefined,
beforeFirstChunk: undefined,
withCredentials: undefined
}
Papa.parse(file, config)
}
render() {
const {
addRoster
} = this.props;
return ( <
Grid >
<
Row >
<
MyDropzone onFileDrop = {
this.fileDrop.bind(this)
}
/> < /
Row > <
/Grid>
)
}
}
export default connect(null, {
addRoster
})(Roster);
【问题讨论】:
-
运行代码sn -p,出现错误
"message": "Uncaught SyntaxError: Unexpected token <", -
抱歉,sn-p 不应该是可运行的。我只是使用它,因为我无法让括号格式工作。 @ppz
-
您能否在
printFile函数中使用console.log(this) 来了解范围。我假设,这个回调是用预定义的上下文调用的
标签: javascript reactjs scope react-redux