【发布时间】:2019-11-15 05:39:17
【问题描述】:
我正在尝试在函数中设置状态。通常我只会将其转换为箭头函数或将其绑定以解决问题,但是如果我将此特定函数转换为箭头函数,我将无法获得所需的结果(这是用作图层的图像映射响应开放层 6)。我怀疑这是因为 this.response 部分代码位于回调函数内部。
这是代码。
this.makeRequest(newSrc, function() {
const arrayBufferView = new Uint8Array(this.response)
const blob = new Blob([arrayBufferView], { type: 'image/png' })
const urlCreator = window.URL || (window).webkitURL
const imageUrl = urlCreator.createObjectURL(blob)
tile.getImage().src = imageUrl
this.setState({
isLoading: false
})
})
我尝试将其转换为停止 setState 的箭头函数不是函数问题,但不会返回 fe 上的地图。对于上下文,makeRequest 函数是一个新的 XMLHttpRequest
makeRequest = (url, onLoad) => {
this.setState({
isLoading: true
})
const client = new XMLHttpRequest()
client.open('GET', url)
client.responseType = 'arraybuffer'
client.setRequestHeader('Authorization', getCredential())
client.onload = onLoad
client.send()
}
【问题讨论】:
-
如果您可以发布代码的其他部分,例如调用函数的位置和方式,将会很有帮助
-
它是从另一个加载新 ImageWMS 图层的函数调用的。这构成了 OpenLayers 中内置的 imageLoadFunction 的一部分
-
@JosephD。这是一个类组件
-
我相信回调函数应该将响应作为参数,例如
this.makeRequest(newSrc, (response) => { /* should be able to access both the response AND setState in here */ }) -
@Jayce444 谢谢我也试过了,但现在仍然没有显示地图数据:(
标签: javascript reactjs