简体   繁体   English

有没有办法让布局初始化(异步)在页面主体初始化之前出现?

[英]Is there a way to have the layout initialization(async) come before the page body initialization?

Today I was surpriced by this finding.今天我对这个发现感到惊讶。 Blazor Layout call on OnInitializedAsync() is being called after the Pages own OnInitializedAsync() and this comes with issues when I want to get, for example, the user settings and have them cached in localStorage so each page later-on can gather and act upon this settings appropriately. Blazor 在页面自己的OnInitializedAsync()之后调用OnInitializedAsync()上的布局调用,当我想要获取用户设置并将它们缓存在localStorage中时会出现问题,以便以后每个页面都可以收集和执行在此设置上适当。

Is it supposed to be like this?它应该是这样的吗? And if yes, is there any workaround to make it so that the Layout initialization ends before committing to the initialization of pages?如果是,是否有任何解决方法可以使布局初始化在提交页面初始化之前结束?

Or there is another possibility, which is: given that all this calls are asyncronous(both the savings to the localStorage and the GET of the user settings) there might be a timeframe where one task is completed quicker than the other.或者还有另一种可能性,即:鉴于所有这些调用都是异步的( localStorage的节省和用户设置的GET ),可能存在一个任务完成得比另一个任务更快的时间范围。

If you want to keep the same approach, then you could try using a bool to determine when to render the @body tag in the layout.如果您想保持相同的方法,那么您可以尝试使用 bool 来确定何时在布局中呈现 @body 标记。

For example:例如:

@if(_isInitComplete)
{
    @body
}


@code {
    private bool _isInitComplete = false;

    protected override async OnInitializedAnsyc()
    {
       // Do stuff
       _isInitComplete = true;
    }
}

I figured out in the end that the best place is at the authentication step, where I can receive this extra data, save it to localStorage and then it can be safely got on each authorized page.最后我发现最好的地方是在身份验证步骤,在那里我可以接收这些额外的数据,将其保存到localStorage ,然后可以安全地在每个授权页面上获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM