Exploring components
In Blazor, a component
is a .razor
file containing a small, isolated functionality (code and markup), or it can be used as a page. A component can host other components as well. This chapter will show us how components work and how to use them.
There are three different ways we can create a component:
- Using Razor syntax, with the code and HTML sharing the same file
- Using a code-behind file together with a
.razor
file - Using only a code-behind file
In this chapter, we will go through the different options. First, we’ll go through the components in the template we used to create the project; these all use the first option, .razor
files, where we have a mix of code and HTML in the same file.
The components in the template are as follows:
- Counter
- Weather
Counter
The counter
page shows a button and a counter; if we click the button, the counter increases. We will now break the page apart, making...