简体   繁体   English

在Blazor中的多个cshtml页面中共享单个属性

[英]Sharing a single property across multiple cshtml pages in Blazor

I'm struggling to share a properties value across components in Blazor. 我正在努力在Blazor的各个组件之间共享属性值。 Basically I have a button with an onclick event this sets a value on a function depending on the value another cshtml page should react to the value and it doesn't. 基本上,我有一个带有onclick事件的按钮,该按钮根据另一个cshtml页面应对该值做出反应的值来设置函数的值,而不会。 The issue is that because both cshtml pages inherit the same function they have their own instance of the function. 问题在于,因为两个cshtml页面都继承相同的函数,所以它们具有各自的函数实例。 Here's my code so far: 到目前为止,这是我的代码:

the Function: 功能:

public class MenuFunctions : BlazorComponent, IMenuFunctions
{

    public bool CollapseNavMenu
    {
        get ; set;

    }

    public void ToggleNavMenu()
    {
        CollapseNavMenu = !CollapseNavMenu;
    }
}

The main button on NavMenuToggleComponent.cshtml: NavMenuToggleComponent.cshtml上的主按钮:

@inherits MenuFunctions 

<div class="pl-4  navbar navbar-dark">
   <button class="navbar-toggler navbar-brand main-button" title="MENU" onclick=@ToggleNavMenu>
    MENU
   </button>
</div>

my NavMenu.cshtml file: 我的NavMenu.cshtml文件:

@inherits MenuFunctions


<div class="@(CollapseNavMenu ? "collapse" : null)" onclick=@ToggleNavMenu>
<ul class="nav flex-column">
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="home">
            <span class="oi oi-home" aria-hidden="true"></span> Home
        </NavLink>
    </li>
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="frontpage">
            <span class="oi oi-home" aria-hidden="true"></span> Front Page
        </NavLink>
    </li>
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="counter">
            <span class="oi oi-plus" aria-hidden="true"></span> Counter
        </NavLink>
    </li>
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="" Match=NavLinkMatch.All>
            <span class="oi oi-account-login" aria-hidden="true"></span> Login
        </NavLink>
    </li>
</ul>
</div>

putting it all together in my MainLayout.cshtml 将它们放到我的MainLayout.cshtml中

@inherits BlazorLayoutComponent

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

<div class="main">

<div class="top-row px-4">
    <a href="http://blazor.net" target="_blank" class="ml-md-auto">About</a>
    <NavLink class="nav-link pull-right" href="logout">
        <span class="oi oi-account-logout" aria-hidden="true"></span> Logout
    </NavLink>
</div>

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

so when I click the button in NavMenuToggleComponent.cshtml I want the CollapseNavMenu to react in the NavMenu.cshtml file 因此,当我单击NavMenuToggleComponent.cshtml中的按钮时,我希望CollapseNavMenu在NavMenu.cshtml文件中作出反应

If I remove the use of the interface, then inject the components onto the pages as opposed to Inherit and then add the following into the startup: 如果我取消使用该接口,则将组件(而不是继承)注入到页面上,然后将以下内容添加到启动中:

services.AddSingleton<MenuFunctions>();

the pages will load, but I still get the same issue. 页面将加载,但是我仍然遇到相同的问题。

The solution here would be to bring the shared property to the place where it is common, in this case the MainLayout.cshtml and not make the components dependent on a common interface. 此处的解决方案是将共享属性带到通用位置,在这种情况下为MainLayout.cshtml而不是使组件依赖于通用接口。

So the MainLayout.cshtml would hold the state in a new property and that property is bound to the two components. 因此MainLayout.cshtml将状态保留在新属性中,并且该属性绑定到两个组件。

You should not use DI for component classes, only for service providing classes and alike. 不要将DI用于组件类,而只能用于服务提供类。

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

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