简体   繁体   English

如何使用 Blazor 创建 MainLayout 以刷新当前页面?

[英]How to create a MainLayout for refreshing the current page using Blazor?

My MainLayout contains a horizontal menu as well as a button on the top of the screen.我的 MainLayout 包含一个水平菜单以及屏幕顶部的一个按钮。 The button brings up a modal for the user to make a selection.该按钮会弹出一个模式供用户进行选择。 After it closes I need the current page to refresh because their selection affects key state which affects all pages.关闭后,我需要刷新当前页面,因为它们的选择会影响影响所有页面的键 state。

I am calling this.StateHasChanged();我打电话给 this.StateHasChanged(); in the MainLayout page.在 MainLayout 页面中。 This does cause its content to refresh.这确实会导致其内容刷新。 However, it does not cause the content of the page contained in the @Body to refresh.但是,它不会导致 @Body 中包含的页面内容刷新。 I have looked quite a bit and don't see how to make this happen.我看了很多,不知道如何做到这一点。

Either pass the values down via a CascadingValue, because that will cause consumers to render when the value changes - or if you are relying on state external to the components (such as in a service) then you'll need to put an event on there that is triggered when your state changes, and your consuming components will have to subscribe and call InvokeAsync(StateHasChanged)通过 CascadingValue 向下传递值,因为这将导致消费者在值更改时呈现 - 或者如果您依赖组件外部的 state(例如在服务中),那么您需要在那里放置一个事件当您的 state 更改时触发,您的消费组件将必须订阅并调用InvokeAsync(StateHasChanged)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM