从应用程序中的其他位置更新其他Blazor组件 [英] Updating other Blazor components from elsewhere in the application

查看:229
本文介绍了从应用程序中的其他位置更新其他Blazor组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑一个具有动态导航(例如,奶酪类别列表)的场景.导航组件存在于布局中,该布局具有各种其他内容组件.其中之一允许用户更新所选奶酪类别的名称.但是...我们如何将其反映在导航组件上?如果将其视为一棵树,则必须(以代码形式)向上导航至布局,然后向下导航至导航组件,在该组件中可以更新状态(通过查询服务器或传递更新的内容) ).

Consider a scenario where you have a dynamic navigation (a list of cheese categories, let's say). The navigation component exists on the layout, which has various other content components. One of those allows the user to update the name of the selected cheese category. But...how do we reflect that back on the navigation component? If one viewed it as a tree you'd have to navigate (in code) up to the layout, then navigate down to the the navigation component where the state can be updated (either by querying the server, or being passed what the update is).

推荐答案

通常,参数流是向下的,即从父级流到 子,而不是其他方向,因为渲染流程进入了 那个方向.这就是为什么没有办法传递参数的原因 上游(例如布局),因为那时没有单个 定义的渲染顺序.

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.

SteveSanderson

SteveSanderson

通常来说,您无法将数据从嵌入式组件传递到MainLayout.

Generally speaking, you cannot pass data from the embedded components to MainLayout.

但是(您的问题很模糊,应该对组件进行更详细的描述),您可以定义一个AppState服务,该服务可以处理所涉及组件的状态,并允许从这些组件访问状态数据

However (your question is quite vague, and you should have given a more detailed depiction of the components), you may define an AppState service that can handle the states of the components involved, and enables access to state data from those components.

请参阅由史蒂夫·桑德森(Steve Sanderson)编写的此示例,如何实现AppState服务.它还演示了我接下来要说的内容: https://github.com/aspnet/samples/tree/master/samples/aspnetcore/blazor

See this sample authored by Steve Sanderson how to implement an AppState service. It also demonstrate what I'm going to say next: https://github.com/aspnet/samples/tree/master/samples/aspnetcore/blazor

将数据从子组件传递到父组件的另一种方法是,在父组件中定义一个可能具有参数的方法,并通过事件处理程序从子组件中调用该方法.

Another way to pass data from a child component to a parent component is by defining a method that may have parameters, in a parent component, and is called from a child component via event handlers.

如果您向我们提供组件的轮廓,则可以提供更具体的答案...

A more specific answer can be provided if you furnish us with the outlines of your components...

希望这对您有帮助...

Hope this helps...

这篇关于从应用程序中的其他位置更新其他Blazor组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆