Blazor正在擦除路由参数OnInitializedAsync


  1. 设置参数同步名称:Id
  2. 设置参数同步值:e4b049b4-0f21-41d5-8fd2-b3349fdd5b58
  3. **调用了OnInitializedAsync OnInit,ID为e4b049b4-0f21-41d5-8fd2-b3349fdd5b58
  4. OnInitializedAsync OnInit被调用,ID为null

当我在Blazor Server项目中使用NavigationManager.NavigateTo导航到具有类似的路由参数的组件时

@页面"/deal/{Id}/edit";,在对OnInitializedAsync的第二次调用时,Id参数被擦除为null

导航到方法:

public void EditDeal()
{
NavigationManager.NavigateTo($"/deal/{dealId}/edit");
}
<a @onclick="EditDeal"

OnInitializedMethod:

protected override async Task OnInitializedAsync()
{
Console.WriteLine($"OnInitializedAsync OnInit called with ID: {Id}");
if (Id == null)
{
dealInformation = dealInformation.SetDefaults();
}
else
{ // Get from DB
dealInformation = await DealService.Get(Id);
}
}

我曾尝试将服务器渲染从ServerPrerender更改为Server,但没有成功,已经被困了几个小时。

谢谢!

作为您唯一向我们展示的部分代码,这里是我为您的问题进行的测试设置,它按预期工作。

GUIDRouteComponent.razor

@page "/deal/{Id:guid}/edit"
<h3>GUID Route Component</h3>
<div class="p-2 m-2">
@this.Id
</div>
@code {
[Parameter] public Guid Id { get; set; }
}

我的索引页面:

@page "/Index"
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<div class="p-2">
<button class="btn btn-primary" @onclick="this.GoToDeal">Go to Deal</button>
</div>
<div class="p-2">
<a href="/deal/e4b049b4-0f21-41d5-8fd2-b3439fdd5b58/edit"> Go to Deal</a>
</div>

@code {
[Inject] private NavigationManager? NavManager { get; set; }
protected override void OnInitialized()
=> base.OnInitialized();
private void GoToDeal()
=> this.NavManager?.NavigateTo("/deal/e4b049b4-0f21-41d5-8fd2-b3439fdd5b58/edit");
}

你说";在对OnInitializedAsync的第二次调用中,Id参数被擦除为null"如果您正在应用程序中导航;应用程序不应该有第二次调用。这只发生在初始页面加载时。

相关内容

  • 没有找到相关文章

最新更新