收听在另一个视图中触发的事件

Listen to an event fired in another view

如何在从另一个视图触发事件的视图中添加侦听器?

查看:

fireEvent(new SelectionEvent(this, false));

应用布局:

addListener(AnotherView.SelectionEvent.class, e -> setTabsEnabled(true));

View 将 AppLayout 作为路由目标组件的父组件。

事件已触发,但侦听器未收到执行表达式的事件。当两者在同一视图中执行但未分开时,此模式工作正常。

如果我理解正确,您想在 父布局 中捕获事件,在本例中为 AppLayout。

您可以使用 UI(对应于 <body> 标签)作为事件总线。 为此,您可以使用以下代码:

// AnotherView
ComponentUtil.fireEvent(ui, new MyAppLayout.SelectionEvent(ui, false));
// AppLayout
ComponentUtil.addListener(ui, SelectionEvent.class, e -> setTabsEnabled(true));

请注意,我已将事件从触发事件的特定视图移至通用应用程序布局 class,因此 MyAppLayout NOT 需要依赖于子视图。另一方面,AnotherView 已经依赖于 @Route 注释中定义的 MyAppLayout。