简体   繁体   English

刷新Blazor布局

[英]Refresh Blazor layout

I got 2 components with AuthorizeView:我使用 AuthorizeView 获得了 2 个组件:

MainLayout:主布局:

<AuthorizeView>
    <Authorized>
        <HeaderMenu />
        <NavMenu />
        <main class="w-full">
            @Body
        </main>
    </Authorized>
    <NotAuthorized>
        @Body
    </NotAuthorized>
</AuthorizeView>

NavMenu (contains the following piece of code): NavMenu(包含以下代码):

<AuthorizeView Roles="@(Role.SuperAdminRoleString + "," + Role.AdminRoleString)">
    <Authorized>
        <NavLink href="@ViewPaths.RegisterPage">
            <span >Register a user</span>
        </NavLink>
    </Authorized>
</AuthorizeView>

NavMenu is MainLayout's child, it's not shown if user is not authorized, after a successful login the AuthorizeView of MainLayout works correctly and shows NavMenu but NavMenu's AuthorizeView is not working unless I refresh the page NavMenu 是 MainLayout 的子项,如果用户未授权,则不会显示,成功登录后 MainLayout 的 AuthorizeView 正常工作并显示 NavMenu 但 NavMenu 的 AuthorizeView 不工作,除非我刷新页面

I tried to call StateHasChanged() for NavMenu from MainLayout but it's not working, also force reload on redirection but it has a weird behavior (NavMenu is appearing before reload)我试图从 MainLayout 为 NavMenu 调用 StateHasChanged() 但它不起作用,还强制重新加载重定向但它有一个奇怪的行为(NavMenu 在重新加载之前出现)

I'm guessing at this point, but it looks like you probably don't have CascadingAuthenticationState set in App.razor .我猜在这一点上,但看起来你可能没有在App.razor中设置CascadingAuthenticationState

Here's an example:这是一个例子:

@namespace Blazr.Auth
<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(App).Assembly" PreferExactMatches="@true">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                <Authorizing><h3>Trying to authorize you.</h3></Authorizing>
                <NotAuthorized><h3>Sorry mate, you can't go here now!</h3></NotAuthorized>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

AuthorizeView instances set it up as a parameter AuthorizeView实例将其设置为参数

[CascadingParameter] private Task<AuthenticationState>? AuthenticationState { get; set; }

When it changes, their SetParametersAsync methods are called by the Renderer, triggering a render event and updating their display.当它发生变化时,渲染器调用它们的SetParametersAsync方法,触发渲染事件并更新它们的显示。

See - https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-6.0请参阅 - https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-6.0

i solve this error by delete cascadingParametre and i replace them @inject AuthenticationStateProvider AuthenticationStateProvider and must be access to authenticate state by instace like this:我通过删除 cascadingParametre 解决了这个错误,我将它们替换为@inject AuthenticationStateProvider AuthenticationStateProvider并且必须通过如下方式访问以验证 state:

 var aut = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        if (aut.User.Identity.IsAuthenticated)
        {
            navigationManager.NavigateTo(ReturnUrl);
        }

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

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