A component is a self-contained chunk of user interface (UI), such as a page, dialog, or form. Blazor applications are created using components which are flexible, lightweight, and can be nested, reused, and shared between projects.
A component in Blazor ends up as a class including both the HTML markup to render along with the processing logic needed to inject data or respond to UI events.
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>
@functions {
int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
}
Members of the component class are defined in a @functions
block, and you can use more than one @functions
block in a component.
@functions
block, component state such as properties and fields are specified along with methods for event handling or for defining other component logics.