how to pass type from one component to another in Blazor code example

Example 1: how to pass property between blazor components

@page "/page2"
@inject Services.AppData AppData

Example 2: 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 3: how to pass property between blazor components

@inject Services.AppData AppData
<div style="width:300px; height:100px; background-color:@AppData.Color">
    <h3>Display Component</h3>
    <p>You entered the number @AppData.Number.</p>
</div>

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

Example 4: how to pass property between blazor components

@page "/page2"
@inject Services.AppData AppData

<h1>Second Page</h1>
<div style="width:640px; height:480px; background-color:@AppData.Color">
    <p>You entered the number @AppData.Number.</p>
</div>

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

Example 5: how to pass property between blazor components

@page "/"
@inject Services.AppData AppData

Tags:

Misc Example