Component Structure

Learn the code structure of Razor components in Blazor WebAssembly.

We'll cover the following...

The following diagram shows code from the Counter component of the Demo project that we will create in this chapter:

Press + to interact
Component structure
Component structure

The code in the preceding example is divided into three sections:

  • Directives
  • Markup
  • Code block

Each of the sections has a different purpose.

Directives

Directives are used to add special functionality, such as routing, layout, and dependency injection. They are defined within Razor and we cannot define own own directives.

In the preceding example, there is only one directive used—the @page directive. The @page directive is ...