how to get one component value in another component in asp.net blazor code example

Example 1: how to pass property between blazor components

@page "/"
@using AppDataService.Components
@inject Services.AppData AppData

<h1>Blazor Singleton Test</h1>
<InputComponent />
<DisplayComponent />
<a href="/page2">Go to Page 2</a>

@code {
    protected override void OnInitialized()
    {
        AppData.OnChange += MyEventHandler;
    }

    private void MyEventHandler()
    {
        Console.WriteLine("AppData changed.");
    }
}

Example 2: how to pass property between blazor components

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<Services.AppData>();
}

Tags:

Misc Example