谷歌地图与ReactJs
问题描述:
我正在使用谷歌地图与反应。但是,当我跑我的项目,我得到了Polygon.js错误文件谷歌地图与ReactJs
TypeError: Cannot read property 'Component' of undefined
TypeError: Cannot read property 'array' of undefined
请让我知道为什么吗?
这是剪断代码和图片描述错误:
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types'
import { GoogleAPIWrapper, InfoWindow, Marker } from 'google-maps-react'
export class MapContainer extends React.Component {
componentDidMount() {
this.loadMap();
}
loadMap() {
let map = new window.google.maps.Map(document.getElementById('map'), {
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13,
mapTypeID: 'roadmap'
})
}
render() {
const style = {
width: "100%",
hight: "100%"
};
return (
<div id='map'>
</div>
);
}
}
export default GoogleAPIWrapper(
{
apiKey: "MY_KEY",
}
)(MapContainer);
答
看来你在代码中有一些错误, 继docs,你不应该甚至能够导出你的组件(在'GoogleAPIWarapper'中输入错字)。
工作示例
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';
import React, { Component} from 'react';
export class MapContainer extends Component {
render() {
return (
<Map google={this.props.google} zoom={14}>
<Marker onClick={this.onMarkerClick}
name={'Current location'} />
<InfoWindow onClose={this.onInfoWindowClose}>
<div>
<h1>Test</h1>
</div>
</InfoWindow>
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: 'apiKey'
})(MapContainer)
+0
Tks。我的项目工作,这是我的错。因为我不明白它的活动方式。 – Foxes
是吧'GoogleAPIWarapper'或'GoogleAPIWrapper'? AND不是你在构造函数中绑定'loadMap()'吗? – Dane
嗨丹尼! Sr,我是GoogleAPIWarapper(GoogleAPIWrapper)中的错误类型。我已经得到新的错误“类型错误:对象(...)不是一个函数”在行:出口默认GoogleAPIWrapper( { apiKey:“MY_KEY” } )(MapContainer); – Foxes