从应用程序的其他地方更新其他 Blazor 组件
Updating other Blazor components from elsewhere in the application
考虑一个场景,其中您有一个动态导航(比方说奶酪类别列表)。导航组件存在于布局中,其中包含各种其他内容组件。其中之一允许用户更新所选奶酪类别的名称。但是......我们如何将其反映在导航组件上?如果有人将其视为一棵树,则您必须向上导航(在代码中)到布局,然后向下导航到可以更新状态的导航组件(通过查询服务器,或传递更新是什么).
In general the parameter flow goes downwards, i.e., from parent to
child, not in the other direction, because the rendering flow goes in
that direction. That's why there isn't a way to pass parameters
upstream (e.g., to a layout), because then there's be no single
defined render order.
史蒂夫桑德森
一般来说,您不能将数据从嵌入式组件传递到MainLayout。
但是(你的问题很模糊,你应该对组件进行更详细的描述),你可以定义一个 AppState 服务来处理所涉及组件的状态,并允许从中访问状态数据这些组件。
查看这个由 Steve Sanderson 编写的示例,了解如何实施 AppState 服务。它还演示了我接下来要说的内容:https://github.com/aspnet/samples/tree/master/samples/aspnetcore/blazor
另一种将数据从子组件传递到父组件的方法是在父组件中定义一个可能具有参数的方法,并通过事件处理程序从子组件调用。
如果您向我们提供组件的轮廓,我们可以提供更具体的答案...
希望这对您有所帮助...
考虑一个场景,其中您有一个动态导航(比方说奶酪类别列表)。导航组件存在于布局中,其中包含各种其他内容组件。其中之一允许用户更新所选奶酪类别的名称。但是......我们如何将其反映在导航组件上?如果有人将其视为一棵树,则您必须向上导航(在代码中)到布局,然后向下导航到可以更新状态的导航组件(通过查询服务器,或传递更新是什么).
In general the parameter flow goes downwards, i.e., from parent to child, not in the other direction, because the rendering flow goes in that direction. That's why there isn't a way to pass parameters upstream (e.g., to a layout), because then there's be no single defined render order.
史蒂夫桑德森
一般来说,您不能将数据从嵌入式组件传递到MainLayout。
但是(你的问题很模糊,你应该对组件进行更详细的描述),你可以定义一个 AppState 服务来处理所涉及组件的状态,并允许从中访问状态数据这些组件。
查看这个由 Steve Sanderson 编写的示例,了解如何实施 AppState 服务。它还演示了我接下来要说的内容:https://github.com/aspnet/samples/tree/master/samples/aspnetcore/blazor
另一种将数据从子组件传递到父组件的方法是在父组件中定义一个可能具有参数的方法,并通过事件处理程序从子组件调用。
如果您向我们提供组件的轮廓,我们可以提供更具体的答案...
希望这对您有所帮助...