如何取消注册.on(drawend,....)事件

问题描述 投票:0回答:1

如下面发布的代码所示,我有一个事件

#drawEvt
并且它已注册为
drawend
。我遇到的问题是取消注册该事件。如下代码所示 在方法
deattachAll()
中,我取消注册它,但它似乎不起作用,因为
evtListenerOnDrawEnd
体内的日志会显示多次。

还有一个重要的注意事项,该算法的设计是为了在

digitize()
之后调用
deattachAll()
。换句话说,最初
digitize()
被调用并且永远不会被调用 再次第二次,除非之前调用
deattachAll()

请告诉我为什么侦听器

#evtListenerOnDrawEnd
被多次调用以及如何修复它

代码

#evtListenerOnDrawEnd = (evt, mapBuilderInstance, rasterLayer) => {
    ...
    ...
    ...
}

getDrawGeomForSource(src, geomType) {
    return new Draw({
        source: src,
        type: geomType,
    });
}

his.#drawEvt = this.#mapBuilderInstance.getDrawGeomForSource(this.#vectorSource, 'Polygon');// to init this.`#drawEvt`

digitize() {
    this.#drawEvt.on('drawend', (evt) => this.#evtListenerOnDrawEnd(
        evt, this.#mapBuilderInstance, this.#rasterLayer
    ));
}

deattachAll() {
    this.#unregisterEvtListeners();
}

#unregisterEvtListeners() {
    this.#mapBuilderInstance.unregisterEvtListener('drawend', this.#drawEvt, this.#evtListenerOnDrawEnd);
}

unregisterEvtListener(eventName, type, callbackListener) {
    if (type) {
        type.un(eventName, callbackListener);
    }
}
openlayers openlayers-3 openlayers-6 openlayers-5 angular-openlayers
1个回答
0
投票

如果回调是一个命名函数,您可以使用

draw.on('drawend', handler);

draw.un('drawend', handler);

如果是匿名函数,您将需要

key = draw.on('drawend', handler);

unByKey(key);
© www.soinside.com 2019 - 2024. All rights reserved.