Updating other Blazor components from elsewhere in the application

你。 提交于 2019-12-11 15:52:04

问题


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).


回答1:


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

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

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.

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...



来源:https://stackoverflow.com/questions/56886281/updating-other-blazor-components-from-elsewhere-in-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!