We could use Cascading values and parameters to achieve the global variable feature, we could define the cascading value in the root(MainLayout component) so that we could use it in any other child components.
I followed the document to have codes like below.
@inherits LayoutComponentBase
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<CascadingValue Value="@golbalVaule">
<article class="content px-4">
@Body
</article>
</CascadingValue>
</main>
</div>
@code{
GolbalValue golbalVaule = new GolbalValue { id = 1, name = "user1" };
}
and this is my index component.
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<div>@golbalValue.name</div>
@code{
[CascadingParameter]
public GolbalValue? golbalValue { get; set; }
}