简体   繁体   English

有什么方法可以与 blazor 页面的主要布局进行通信

[英]Is there any way to communicate to main layout of blazor pages

Here I want to pass some title to the mainlayout.razor page so that my components or pages can update the title of header.这里我想将一些标题传递给 mainlayout.razor 页面,以便我的组件或页面可以更新 header 的标题。 Is there any possible way in blazor to do this? blazor 中是否有任何可能的方法来做到这一点? Any help will be appreciated.任何帮助将不胜感激。

@inherits LayoutComponentBase

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <div class="main">
        <div class="top-row px-4 bg-success">
            <button onclick="toggleNav()">
                <span class="oi oi-menu"></span>
            </button>
            <div class="text-center">
                hey there
            </div>
        </div>

        <div class="content px-4">
            @Body
        </div>
    </div>
    
</div>

@code
{

}

and my child component is a router page with @page directive to navigate between pages i want that component to update this mainlayout is there any possible way?我的子组件是一个带有@page 指令的路由器页面,用于在页面之间导航我希望该组件更新这个主布局有什么可能的方法吗? Thanks in advance for any help.提前感谢您的帮助。

Regards,问候,

You should wrap the view portion of the MainLayout component with the CascadingValue component whose value is a reference to the MainLayout itself, so that you can reference the MainLayout component, say, from the Counter component, from which you assign a value string to the property Title defined in the MainLayout component.您应该使用 CascadingValue 组件包装 MainLayout 组件的视图部分,该组件的值是对 MainLayout 本身的引用,以便您可以引用 MainLayout 组件,例如,从 Counter 组件中,您可以从中为属性分配值字符串在 MainLayout 组件中定义的标题。 This property also contain a call to the StateHasChanged method to refresh the display...此属性还包含对 StateHasChanged 方法的调用以刷新显示...

 @page "/counter"



// Gets a reference to the MainLayout component
    [CascadingParameter]
    public MainLayout Layout { get; set; }


    protected override void OnInitialized()
    {
        Layout.Title = "Greetings from Counter";

    }

MainLayout.razor MainLayout.razor

@inherits LayoutComponentBase

<CascadingValue Value="this">
    <div class="page">
        <div class="sidebar">
            <NavMenu />
        </div>

        <div class="main">
            <div class="top-row px-4">
                @Title
                <LoginDisplay />
                <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
            </div>

            <div class="content px-4">
                @Body
            </div>
        </div>
    </div>

</CascadingValue>

@code
{
    private string title;

    public string Title
    {
        get => title;
        set
        {
            title = value;
            InvokeAsync(() => StateHasChanged());
        }
    }
}

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

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