-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
为什么在created中拿不到AMap的实例,总是报错no defined?求解答,感谢. #41
Comments
请提供更详细的信息,比如使用的 react-amap 版本,重现的 jsfidddle 链接或者代码之类的。 |
@ioslh 版本 "react-amap": "^1.0.3",
render函数中:code
然后我在created中就会报错 AMap no defined <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app" style="width:500px;height:500px;">
<example></example>
</div>
<!--<script src="{{ mix('/js/app.js') }}"></script>-->
<script src="https://cdn.bootcss.com/react/15.6.1/react.min.js"></script>
<script src="https://cdn.bootcss.com/react/15.6.1/react-dom.min.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/react-amap@0.2.6/dist/react-amap.js"></script>
<script type="text/babel">
var Map = ReactAMAP.Map;
var Marker = ReactAMAP.Marker;
var Markers = ReactAMAP.Markers;
class MyMap extends React.Component {
constructor(){
super();
const _this = this;
this.map = null;
this.marker = null;
this.geocoder = null;
this.mapEvents = {
created(map){
_this.map = map;
console.log(_this.map)
console.log(AMap)
AMap.plugin('AMap.Geocoder',() => {
_this.geocoder = new AMap.Geocoder({
city: "010"//城市,默认:“全国”
});
})
},
click(e){
const lnglat = e.lnglat;
_this.setState({
position: lnglat,
currentLocation: 'loading...'
});
_this.geocoder && _this.geocoder.getAddress(lnglat, (status, result) => {
console.log(result);
if (status === 'complete'){
if (result.regeocode){
_this.setState({
currentLocation: result.regeocode.formattedAddress || '未知地点'
});
} else {
_this.setState({
currentLocation: '未知地点'
});
}
} else {
_this.setState({
currentLocation: '未知地点'
});
}
})
}
};
this.markerEvents = {};
this.state = {
position: {longitude: 120, latitude: 30},
currentLocation: '点击地图'
}
}
render(){
return <Map center={this.state.position} events={this.mapEvents}>
<Marker position={this.state.position} events={this.markerEvents} />
<div className="location">{this.state.currentLocation}</div>
</Map>
}
}
ReactDOM.render(<MyMap/>, document.getElementById('app'));
</script>
</body>
</html> |
window.AMap 试试。 注意内容排版。 |
@ioslh 刚刚在看其他人的问题提问 发现了用window 然后试了下可以了 刚好又看到你回复了 谢谢 |
原因是啥啊 为啥有的时候需要windows.AMap |
@America-first-melon 感觉这是 eslint 这样的代码检测工具的报错,为了防止使用未定义的变量带来错误。我用 create-react-app 生成一个应用之后,引入 Map 并且直接使用 AMap(没有 window),控制台会报错,但是代码仍然运行了。 |
@ioslh 啊 谢谢 这个插件很好用 |
react-amap version
OS/Browers version
Reproduction Link
Steps to reproduce
What is Expected?
What is actually happening?
The text was updated successfully, but these errors were encountered: