OnParametersSetAsync的执行方式和执行次数如何。

huangapple go评论69阅读模式
英文:

How is OnParametersSetAsync executed and the number of executions

问题

If there are two parameter changes in the Blazor server, will OnParametersSetAsync be executed once after each change or will OnParametersSetAsync be executed once after both changes are executed. Why does OnParametersSetAsync execute twice when the homepage is first loaded?

我认为Blazor的生命周期有点混乱,比如不确定的OnParametersSetAsync执行次数,OnInitialized执行两次并在第二次重置,以及有时没有真正参数的OnAfterRender。总之,它非常混乱和令人沮丧。

英文:

If there are two parameter changes in the blazer server, will OnParametersSetAsync be executed once after each change or will OnParametersSetAsync be executed once after both changes are executed. Why does OnParametersSetAsync execute twice when the homepage is first loaded

I think the lifecycle of a blazer is a bit chaotic, such as the uncertain number of OnParametersSetAsync executions, OnInitialized executions twice and resetting the second time, and OnAfterRender sometimes without a true parameter. In short, it's very chaotic and frustrating

答案1

得分: 1

以下是翻译好的内容:

流程相当清晰:

OnParametersSetAsync的执行方式和执行次数如何。

OnParametersSet将在每个渲染周期内执行一次,与参数的数量无关,包括零个参数。

在Blazor Server上看到它发生两次是因为RenderMode="ServerPrerendered"

如果不想进行预渲染,请切换到ServerRendered。

英文:

The flow is pretty clear:

OnParametersSetAsync的执行方式和执行次数如何。

OnParametersSet will execute once per render cycle, independent of the number of parameters. Including zero parameters.

You see it happening twice on Blazor Server because of RenderMode="ServerPrerendered"

Switch to ServerRendered if you don't want prerendering.

答案2

得分: 0

Sure, here are the translated parts:

如果在 blazer 服务器中有两个参数更改,OnParametersSetAsync 会在每次更改后执行一次,还是在两个更改都执行后执行一次?

如果在组件渲染后设置参数值,不会触发子组件上的 OnParametersSetAsync

只有在父组件由于 UI 事件处理程序调用或手动调用 StateHasChanged 的结果时才会发生。StateHasChanged 将一个呈现片段的形式排队到渲染器的队列中。渲染器执行此呈现片段并检查任何子组件的参数是否已更改。对可能已更改参数的任何组件都会调用 SetParametersAsync

我认为 blazer 的生命周期有点混乱

那是因为您目前没有足够的知识来完全理解它。
这不是一个简单的主题:您不会在一天内掌握组件!我曾经也像你今天一样,所以我理解你的挫败感。

英文:

> If there are two parameter changes in the blazer server, will OnParametersSetAsync be executed once after each change or will OnParametersSetAsync be executed once after both changes are executed?

Setting parameter values in a component once it has rendered will not trigger OnParametersSetAsync on a child component.

That will only happen when the parent component renders either as the result of a UI event handler call or you manually calling StateHasChanged. StateHasChanged queues a render request onto the Renderer's queue in the form of a RenderFragment. The Renderer executes this RenderFragment and checks if the parameters for any sub-components have changed. It calls SetParametersAsync on any components where the parameters may have changed.

> I think the lifecycle of a blazer is a bit chaotic

That's because you don't current have the necessary knowledge to fully understand it.
It's not a simple subject: you won't master components in a day! I was once where you are today, so I understand your fustrations.

huangapple
  • 本文由 发表于 2023年4月17日 19:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定