在我的前端,我正在与Flux调度员一起运行React。我也运行了web-pack-dev服务器。
研究"意外令牌"错误,我不断得出这个解决方案:babel-loader jsx SyntaxError: Unexpected token
但是,我将这个预设包含在我的webpack.config.js文件中,并且只有在来自flux的调度程序中包含一个数组时才会出现此错误。我已经包含了我为以下测试而构建的功能。这只能传递一个对象,但在传递数组时会抛出错误。
Module build failed: SyntaxError: /Users/foo/sites/app/js/actions/TripActions.js: Unexpected token (33:6)
31 | country: "Spain",
32 | complete: false
> 33 | },
| ^
34 | {
35 | id: 987655432,
36 | text: "Another Great Flat!",
我正在测试的功能
export function reloadTrip() {
dispatcher.dispatch({type: "FETCH_TRIPS"});
setTimeout(() => {
dispatcher.dispatch({type: "RECIEVE_TRIPS", [
{
id: 123456789,
text: "Nice flat for you and me",
city: "Madrid",
country: "Spain",
complete: false
},
{
id: 987655432,
text: "Another Great Flat!",
city: "Paris",
country: "France",
complete: true
}
]});
}, 1000);
}
答案 0 :(得分:1)
您正在将对象传递给dispatcher.dispatch
,但该对象具有键/值({type: 'RECEIVE_TRIPS'}
)和数组([{...}, {...}]
)。数组无效,对象需要键/值。
通过:
{
type: 'RECEIVE_TRIPS',
trips: [{...}, {...}],
}
你应该做得更好。
要测试Babel是否按预期工作,请在命令行上尝试使用无错误(即:简单)脚本。