【问题标题】:Aurelia: EventAggregator fires twiceAurelia:EventAggregator 触发两次
【发布时间】:2020-02-20 15:59:13
【问题描述】:

订阅函数调用的函数触发两次。

发布者不是在激活或附加函数中使用,而是在不同类的异步函数中使用。两个类都通过绑定接收相同的 EventAggregator。 Console.Trace() 在这两种情况下都有相同的路由。发布/订阅集是唯一的,不被任何其他类使用。

async sender(item:any):Promise<void> {
this.dialogService.open({
            viewModel: CaModalConfirm,
            model: {
                color:   this.color
            }
        }).whenClosed(async response => {
            if (response.wasCancelled === false) {
                this.moduleName = params.params.moduleId;
                await this.selectionEventAggregator.publish('requestSelection',{item: item});
                this.elementEventAggregator.publish('hideSidebar');
            }
        });
}

---------------------------------------------

    attached() {
        this.subscriptions.push(
            this.selectionEventAggregator.subscribe(
                'requestSelection',
                params => this.sendSelection(params)
            )
        );
    }
    
    sendSelection(params):void {
        console.trace(params);
        this.selectionEventAggregator.publish(
            'sendSelected',
            {
                selection:  this.itemSelection,
                item:      params.item
            }
        );
    }

【问题讨论】:

  • 您更有可能订阅了多个事件,而不是事件触发了两次。分享一些代码 - 我们会尽力提供帮助。
  • @avrahamcool 我更新了我的代码的简化版本。我已经对我的整个代码进行了搜索,以确保没有重复订阅。
  • 多次订阅并不总是意味着您已经在代码中编写了两次。而是因为某种原因你使用了两次。

标签: aurelia aurelia-templating


【解决方案1】:

包含带有订阅的自定义元素的自定义元素已被使用两次,这导致了问题。这不是 EventAggregator 问题。

【讨论】:

    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多