Openlayers 3选择交互无法添加事件条件

Mun*_*erz 6 openlayers typescript openlayers-3 angular

因此,当我将鼠标悬停在任何明显突出显示的功能上时,我正在尝试向其地图中添加选择交互。

import Select from 'ol/interaction/select';
import pointerMove from 'ol/events/condition.js'

this.selectPointerMove = new Select({
   condition: pointerMove
});
this.coreMapComponent.map.addInteraction(this.selectPointerMove);
Run Code Online (Sandbox Code Playgroud)

条件字段抛出错误-

 Type 'typeof events' is not assignable to type 'EventsConditionType'.
 Type 'typeof events' provides no match for the signature '(event: MapBrowserEvent): boolean'.
Run Code Online (Sandbox Code Playgroud)

在没有条件的情况下,单击鼠标即可正常工作。

应该在Angular 6项目中使用“ @ types / ol”:“ ^ 4.6.2”提及(如果有任何意义)。

Llo*_*iol 1

当前的 Openlayers 版本 5.xx 需要一些打字更新。因为即使您使用的是 Openlayers 5.xx,安装的类型也来自版本 4.xx

这意味着您需要在代码中采取一些解决方法。

由于版本 4.xx 上的所有类型都使用DefaultExports方法,因此您不能使用NamedExports,例如:

import {pointerMove} from 'ol/events/condition';
Run Code Online (Sandbox Code Playgroud)

解决方案:

您可以做的一种选择是将全部导入为变量。这样,你就可以避免 TS 错误:

import Select from 'ol/interaction/select';
import * as condition from 'ol/events/condition';

this.selectPointerMove = new Select({
   condition: (condition as any).pointerMove
});
this.coreMapComponent.map.addInteraction(this.selectPointerMove);
Run Code Online (Sandbox Code Playgroud)

这样做的一个副作用是你将删除进行 tree-shaking 的选项,但是好吧,没有它你也能生存。

希望这可以帮助!