js函数嵌套取值问题

free_kxz 2020-04-07 06:00:15
请教各位大神,我在vue开发中遇到,主函数无法取到子函数里面的值的问题。
eMap.prototype.rangeDraw = function(origin, radius, onComplete) {
var o = {};

esriLoader.loadModules(["esri/geometry/Polyline","esri/SpatialReference","esri/geometry/Point","esri/geometry/Circle"],eMap.prototype.options).then(
([Polyline,SpatialReference,Point,Circle]) => {

var map = this.map;
var rangeStyle = {
strokeColor : "#0000CD",
strokeOpacity : 0.5,
strokeWeight : 1,
strokeWidth:1,
pointerEvents : "visiblePainted",
fillColor : "#1C86EE",
fillOpacity : 0.1,
pointRadius : 4
};
var spatialReference=new SpatialReference (eMap.prototype.spatialReference);

this.clearGraphic();
//this.removeLayer(drawLayerName);

var pt=new Point(origin.x,origin.y,spatialReference);

var circleGeometry =new Circle(pt,{
"radius":radius
});
var circleLine=new Polyline(spatialReference);
circleLine.addPath(circleGeometry.rings[0]);
circleGeometry.spatialReference=spatialReference;

const circleGraphic=this.getGraphic(circleLine,rangeStyle);
//this.map.graphics.add(circleGraphic);

var geometry = this.toMyGeometry(circleGeometry);
//var geoStr = "POLYGON(";
o.geoStrTemp="";
for(var k=0;k<geometry.points.length;k++){
o.geoStrTemp=o.geoStrTemp+geometry.points[k].x+" "+geometry.points[k].y+","

}
o.geoStrTemp="POLYGON("+o.geoStrTemp+")";
//console.log("geometrys11111_______________:"+geoStrTemp);
//geoStrTemp1=geoStrTemp;
var promise=new Promise(function (resolve,reject){
resolve(o.geoStrTemp);
})

geometry.style = rangeStyle;
var geometrys = [];
geometrys.push(geometry);
eMap.prototype.showGeometryLayer(this.drawLayerName,geometrys)

this.locator(circleGeometry);
this.addMarker(origin);
if(onComplete){
onComplete(radius,radius,circleGeometry);
}

return o;
}

).catch(err => {
// handle any script or module loading errors
console.error(err);
});
//o.then(res => { console.log("cs_________________"+res.geoStrTemp) })

console.log("geoStrTemp1______________:"+o.geoStrTemp);//这里无法拿到值,请教有没有解决办法。
return o;
};
...全文
235 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
free_kxz 2020-04-07
  • 打赏
  • 举报
回复
就如nian_cj说的一样,谢谢。我得多补补js的功能了
free_kxz 2020-04-07
  • 打赏
  • 举报
回复
谢谢问题。解决了
天际的海浪 2020-04-07
  • 打赏
  • 举报
回复
异步获取数据一般要用回调函数来返回数据, 一定要用return的话可以去看看async function和 await Promise
free_kxz 2020-04-07
  • 打赏
  • 举报
回复
好的 我试试。谢谢大神指点。
_念_ 2020-04-07
  • 打赏
  • 举报
回复
你直接 return你的子函数 子函数的在你的esriLoader.loadModules 前面加个return 然后调用rangeDraw 函数后就会获取到一个promise对象 rangeDraw(参数).then(参数2 => {})参数二就是你esriLoader.loadModules 的then函数里return的o了
free_kxz 2020-04-07
  • 打赏
  • 举报
回复
是的 我在onComplete return 也不行。。不知道有什么解决办法。。。
_念_ 2020-04-07
  • 打赏
  • 举报
回复
你这个是异步的,console执行的时候 内部的函数还没有执行完成 所以拿不到

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧