0
我在使用react + google maps API時遇到了一些麻煩。如何將this.map傳遞到此Google地圖事件中?
我設置的this.map
值在我的構造函數:
constructor(props) {
super(props);
this.map = null;
}
我加載了谷歌地圖API,並在componentWillReceiveProps
方法建立我的地圖:
componentWillReceiveProps({ isScriptLoaded, isScriptLoadSucceed }) {
if (isScriptLoaded && !this.props.isScriptLoaded) { // load finished
console.log('yes - isScriptLoaded && !this.props.isScriptLoaded');
if (isScriptLoadSucceed) {
this.map = new google.maps.Map(this.refs.map, mapOptions);
/* not sure how to do this part */
google.maps.event.addDomListener(window, 'resize', function() {
const center = this.map.getCenter(); //this.map undefined, unless above I add const map = this.map, and then use map.
google.maps.event.trigger(this.map, 'resize');
this.map.setCenter(center);
});
是否有辦法我可以通過this.map
除了創建另一個變量const map = this.map
或什麼東西,並通過?
我很擔心,通過使用const map = this.map
,然後在剛剛map
進一步的修改會導致問題,如果我忘記更新this.map
值:(