我当前的设置:
- .NET Core 3(预览版 6)
- Blazor 服务器端渲染
在 Blazor 页面中,我有类似以下内容:
@page "/page"
@page "/page/{Id}"
With:
[Parameter]
public string Id { get; set; }
现在,例如,当从以下位置导航时:/page/1
to /page/2
, 我可以用OnParametersSetAsync
来检测这些变化。但是当从以下位置导航时就会出现问题/page/1
to /page
. OnParametersSetAsync
可以检测到这种变化,但是Id
参数将保持为1
从之前的路线。
我想知道在这种情况下我能做什么。
Blazor 不会用 null 覆盖空参数,请自行执行:
@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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)