我创建了NavLink
的列表,这只是它们之间的一个不同,动态参数Id
<div>
@foreach (var service in pageGlobal.Person.Services)
{
var link = $"service_description/{service.Identifier}";
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="@link">
@service.Name
</NavLink>
</li>
</ul>
}
</div>
并且仅当我第一次单击时它才起作用(无论它在NavLink的哪个项目中均正常工作)。
单击下一个链接-网址已更改,但没有任何反应。
ServiceDescription.razor
@page "/service_description/{Id:int}"
@using Site.Data
@using Site.ViewModels
@inject StartUpService page
@if (service != null)
{
<div> <h3>Service Name - @service.Name</h3> - <h4>@service.Description</h4> </div>
}
@code
{
[Parameter]
public int Id { get; set; }
PageGlobal pageGlobal;
Service service;
protected override async Task OnInitializedAsync()
{
pageGlobal = await page.GetPageGlobalAsync();
service = pageGlobal.Person.Services.Where(e => e.Identifier == Id).FirstOrDefault();
}
}
如何使用ServiceDescription.razor
功能通过新参数强制客户端重新加载NavLink
?
答案 0 :(得分:1)
此问题是由OnInitializedAsync
引起的。对于OnInitializedAsync
,仅当组件准备启动时才调用该组件时才调用它。
如果要基于参数更改service
,则应使用OnParametersSetAsync
,如下所示:
protected override async Task OnParametersSetAsync()
{
pageGlobal = new PageGlobal
{
Person = new Person()
{
Services = new List<Service>(){
new Service{ Identifier = 1, Name = "Test1", Description = "D1" },
new Service{ Identifier = 2, Name = "Test2" , Description = "D2" },
new Service{ Identifier = 3, Name = "Test3", Description = "D3" }
}
}
};
service = pageGlobal.Person.Services.Where(e => e.Identifier == Id).FirstOrDefault();
}
您可以检查ComponentBase