javascript - TypeError:在 React 组件中使用 Google Location API 时无法读取未定义的属性“位置”
问题描述
我正在尝试将 Google 位置 API 配置为在表单输入中使用。我收到以下错误:
我在 index.html 文件中使用以下脚本标记:
这是我尝试在其中使用 Google API 的组件的代码:
import React from 'react'
/* global google */
const google = window.google = window.google ? window.google : {}
import { FormContainer, ModalTextInput } from './styles'
import '../styles.css'
import { Modal, Button, Form } from 'react-bootstrap'
import TelInput from './IntlTelInput'
class EditDetailsModal extends React.Component {
constructor(props) {
super(props);
this.autocompleteInput = React.createRef();
this.autocomplete = null;
this.handlePlaceChanged = this.handlePlaceChanged.bind(this);
}
componentDidMount() {
this.autocomplete = new google.maps.places.Autocomplete(this.autocompleteInput.current,
{"types": ["geocode"]});
this.autocomplete.addListener('place_changed', this.handlePlaceChanged);
}
handlePlaceChanged(){
const place = this.autocomplete.getPlace();
this.props.onPlaceLoaded(place);
}
render() {
return (
<Modal show={this.props.show} onHide={this.props.handleClose} className="editModal">
<FormContainer>
<Modal.Header className="editModalHeader">
<Modal.Title>Edit Personal Details</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form>
<Form.Group controlId="formBasicName">
<ModalTextInput customPlaceholder="Name"/>
</Form.Group>
<Form.Group controlId="formBasicEmail">
<ModalTextInput customPlaceholder="Email ID"/>
</Form.Group>
<Form.Group controlId="formBasicPhoneNumber">
<TelInput className="formInput"/>
</Form.Group>
<Form.Group controlId="formBasicName">
<ModalTextInput ref={this.autocompleteInput} customPlaceholder="Country you will work in"/>
</Form.Group>
<Button variant="primary" type="submit" className="submitBtn">
Save details
</Button>
</Form>
</Modal.Body>
</FormContainer>
</Modal>
)
}
}
export default EditDetailsModal
我尝试了许多修复程序,但它们似乎不起作用。
解决方案
推荐阅读
- react-native - Sockit.io 无法通过 React Native 连接到服务器
- go - 我可以模拟一个需要使用指针参数的函数吗
- xgboost - 梯度提升分类器问题
- python - 在 Python 中覆盖一小部分代码的最佳实践
- r - R - 将每日格式的日期列转换为每小时粒度
- vue.js - 带有分页的 Vuetify 卡片
- javascript - gs函数的定义是当你从html代码调用函数时?
- reactjs - 通过 ref 从父组件访问功能组件的方法
- firebase - React Native - 使用 @react-native-firebase/storage 将图像上传到 Firebase - 没有创建 Firebase 应用“[DEFAULT]”
- php - 无法使用自动缩放,因为无法在 PHP 中确定 Y 轴的有效最小值/最大值