在组件加载时加载数据 [英] Loading data on component load
问题描述
有人会认为这是一件容易的事.但是,如何在不加载两次的情况下在组件加载时调用服务方法呢?
One would think this is an easy task. But how do I call a service method on component load without it firing twice?
当前-
protected override async Task OnInitializedAsync()
{
await Initialize();
}
private async Task Initialize()
{
Video = await _VideoService.GetAsync(Id);
}
现在,该方法被调用了两次,这对于两次访问相同的信息数据库似乎不是一个好主意.
Right now this gets called twice, which doens't seem like a good idea to hit the database twice for the same information.
我已经尝试过在下面进行此操作,但是它甚至在每次渲染之前都会抛出一个错误.
I've tried just having this below, but it will throw an error before it even renders everytime.
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(firstRender)
{
await Initialize();
}
}
推荐答案
这是设计使然-检查预渲染后的状态重新连接以及本文的其他部分,以找出在您的情况下加载数据的安全方法是什么.也许是 It is by design - check Stateful reconnection after prerendering and other sections of this article to figure out what would be the safe way to load data in your case. Maybe it is 这篇关于在组件加载时加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! SetParametersAsync
或SetParametersAsync
or OnParametersSetAsync