gpt4 book ai didi

blazor - OnInitializedAsync 方法在更改查询字符串值时没有被调用

转载 作者:行者123 更新时间:2023-12-04 16:39:33 25 4
gpt4 key购买 nike

我正在开发 Blazor 服务器端应用程序,并且有一个绑定(bind)动态菜单的标题。还有一个类别页面,我根据已经绑定(bind)在标题菜单中的查询字符串显示数据。

一旦我选择菜单,就会调用 OnInitializedAsync 方法,并且类别页面代表查询字符串值显示数据,但我再次打开菜单并选择不同的产品,然后类别页面不会被调用。

在标题菜单的代码下方。

Header.razor(这是在主布局页面上注册的)

<ul class="cat_menu">
@if (categoryDtos == null)
{
<div>Loading...</div>
}
else
{
@foreach (var category in categoryDtos)
{
<li><a href="category/@category.Id">@category.Name<i class="fas fa-chevron-right"></i></a></li>
}
}
</ul>

public class HeaderBase : ComponentBase
{
[Inject] public IToastService toastService { get; set; }

[Inject] ICategoryService categoryService { get; set; }

public List<CategoryDto> categoryDtos { get; set; }

protected async override Task OnInitializedAsync()
{
var categoryLIst = await categoryService.GetCategories();

if (categoryLIst.Data != null)
{
categoryDtos = categoryLIst.Data.ToList();
toastService.ShowSuccess("Fetch successfully");
}
else
{
toastService.ShowError(categoryLIst.Message);
}
}


}

Product.razor 页面

 @page "/category/{categoryID}"

<ul class="sidebar_categories">

@if (categoryTypeDtos == null)
{
<div>Loading...</div>
}
else
{
@foreach (var category in categoryTypeDtos)
{
<li><a href="#">@category.Name</a></li>
}
}
</ul>

public class CategoryBase : ComponentBase
{
[Inject] public IToastService toastService { get; set; }
[Parameter] public string categoryID { get; set; }

[Inject] ICategoryService categoryService { get; set; }

public List<CategoryTypeDto> categoryTypeDtos { get; set; }

protected async override Task OnInitializedAsync()
{

var response = await categoryService.GetCategoryTypeByID(Guid.Parse(categoryID));
if (response.Data != null)
{
categoryTypeDtos = response.Data.ToList();
toastService.ShowSuccess("Fetch successfully");
}
else
{
toastService.ShowError(response.Message);
}
}
}

最佳答案

OnInitialized 仅在首次创建组件或页面时调用。

呈现相同组件的新 url 将使用该实例。你需要 OnParametersSetAsync 代替。

关于blazor - OnInitializedAsync 方法在更改查询字符串值时没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64124567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com