Blazor route changes in same page

让人想犯罪 __ 提交于 2020-01-15 05:17:48

问题


I'm current setup:

  • .NET core 3 (preview 6)
  • Blazor server side rendering

In a Blazor page I have something like the following:

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

With:

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

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.


回答1:


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;  
    }
}



来源:https://stackoverflow.com/questions/56869508/blazor-route-changes-in-same-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!