同一页面中的Blazor路由更改 [英] Blazor route changes in same page

查看:168
本文介绍了同一页面中的Blazor路由更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的设置是


  • .NET core 3(预览版6)

  • Blazor服务器端渲染

在Blazor页面中,我有以下内容:

In a Blazor page I have something like the following:

@page "/page"
@page "/page/{Id}"

使用:

[Parameter]
public string Id { get; set; }

现在,例如从 / page / 1 / page / 2 ,我可以使用 OnParametersSetAsync 来检测这些更改。但是从 / page / 1 导航到 / page 时会发生问题。 OnParametersSetAsync 可以检测到此更改,但是 Id 参数将保持为 1 来自先前的路线。

Now, when navigating from, for example, /page/1 to /page/2, I can use OnParametersSetAsync to detect these changes. But the problem occurs when navigating from /page/1 to /page. OnParametersSetAsync can detect this change, however the Id parameter will stay as 1 from the previous route.

我想知道在这种情况下我该怎么办。

I was wondering what i could do in this situation.

推荐答案

Blazor不会使用null覆盖空参数,请自己执行:

Blazor doesn't override empty parameters with null, do it by yourself:

@page "/Counter"
@page "/Counter/{Id}"
<h1>Current Id: @IdString;</h1>
<a href="/Counter"> go home </a> |
<a href="/Counter/1"> go home 1 </a> |
<a href="/Counter/2"> go home 2 </a>

@code
{
    private string IdString;
    [Parameter] public string Id { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        // copy par value somewhere
        IdString = Id?.ToString() ?? "";

        // rest parm (set to null) by yourself
        Id=null;  
    }
}

这篇关于同一页面中的Blazor路由更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆