c# - 在初始化组件之前使用异步方法初始化 Blazor 范围服务
问题描述
我有一个范围服务:
services.AddScoped<UserSettingsService>();
我想在子组件初始化之前(之前)在服务上调用异步方法(从数据库加载数据ComponentBase.OnInitialized();
)
打电话的最佳地点在哪里UserSettingsService.LoadAsync();
?
我曾尝试在 OnInitializedAsync() 的 App 组件中执行此操作,但似乎为时已晚,因为我的子组件已在 UserSettingsService 之前初始化:
app.razor.cs:
public partial class App {
[Inject] UserSettingsService UserSettingsService {get; set;}
[Inject] AuthenticationStateService AuthenticationStateService {get; set;}
public override async Task OnInitializedAsync() {
string userName = (await AuthenticationStateService.GetAuthenticationState()).User.Identity.Name;
await UserSettingsService.LoadAsync(userName );
await base.OnInitializedAsync();
}
}
解决方案
据我所知,SetparametersAsync
应该做的伎俩。
图片非常清楚 blazor 组件生命周期: https ://knowledgebasehavit.files.wordpress.com/2019/11/blazor-component-lifecycle-diagram.png
推荐阅读
- swift-mt - SWIFT 规范 - 字段和标签之间有什么区别?
- xampp - 无法启动 XAMPP 和 Tomcat 8.0
- android - AsyncTask 不执行
- python - 当 x 轴包含重复项时,altair/vega-lite 中的 mark_line 重新排序数据
- mongodb - 限制 $lookup 聚合中的字段
- android - 错误类型 3 错误:活动类 {com.myapp/com.myapp.MainActivity} 不存在
- c# - SevenZipSharp 无法解压某些 tar 档案
- c# - 如何在 Unity3D 中快速渲染从 Android 获取的字节数组?
- python - 数据框缺失值显示在每行的末尾
- hadoop - 在 Hive 查询中显示前一个日期的日期名称?