我曾尝试使用react-async-script-loader和react-async-script进行测试,但是当我禁用Internet来测试错误捕获时,我的标记会显示出来,但没有地图,也没有错误。我还添加了如下所示的错误捕获功能,但是每次加载地图时,无论启用还是禁用Internet,它都会首先出现。以下内容直接来自npm软件包,并进行了少量编辑
export class App extends Component {
constructor(props) {
super(props)
this.state = {
map: {},
markers: markers
}
}
componentWillReceiveProps ({ isScriptLoaded, isScriptLoadSucceed }) {
if (isScriptLoaded && !this.props.isScriptLoaded) {
// load finished
if (isScriptLoadSucceed) {
window.initMap()
}
else {this.props.onError(), window.alert('Google maps not loaded, try again')}
}
}
async componentDidMount () {
const { isScriptLoaded, isScriptLoadSucceed } = this.props
if (isScriptLoaded && isScriptLoadSucceed) {
window.initMap()
}
}
render() {
const { markers } = this.state
return (
<div>
<div id="map" />
</div>
)
}
}
export default scriptLoader(
[
'https://maps.googleapis.com/maps/api/js?libraries=places,drawing,geometry&key={my api key}&v=3.32&callback=initMap'
]
)(App)
还说除非我指定窗口,否则未定义initMap函数,我尝试与下面的对象绑定,但是绑定未定义。
this.initMap = this.initMap.bind(this)