简体   繁体   中英

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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