我有一个功能:
handleMarkerClick(targetMarker) {
this.setState({
markers: this.state.markers.map(marker => {
if (marker === targetMarker) {
return {
...marker, // error TS1136: Property assignment expected.
showInfo: true, // error TS1005: ',' expected.
}; // error TS1135: Argument expression expected, error TS1005: ')' expected.
}
return marker; // error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
}), // Declaration or statement expected.
});
}
我看到很多错误:
- 预期进行属性分配。
- 预期为“,”。
- 需要参数表达式。
- 您可能需要适当的加载程序来处理此文件类型。 |导出.__esModule = true; |导出[“默认”] = PopUpInfoWindowExample; |返回标记;
但是当我删除“标记”附近的“...”时,我的功能无法正常工作。
我这样做:
handleMarkerClick(targetMarker) {
this.setState({
markers: this.state.markers.map(marker => {
if (marker === targetMarker) {
return {
marker, // delete ...
showInfo: true,
};
}
return marker;
}),
});
}
为什么?我该怎么做才能获得正确的功能?
最佳答案
如果marker
是一个对象,那么您可以尝试使用Object.assign()
,而不是使用扩展运算符。这是一个例子:
marker = Object.assign({}, marker);
关于javascript - react 和 typescript 上的传播运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325678/