Adding a Component to the Razor Class Library
Learn how to add the Dialog component in the Razor class library and how to use it in the ModalDialog app.
We'll cover the following
In order to share the Dialog
component, we need to move it to the Razor class library that we just created and tested. We do this as follows:
-
Right-click the
ModalDialog\Shared\Dialog.razor
file and select the “Copy” option from the menu. -
Right-click the
MyComponents
project and select the “Paste” option from the menu. -
Right-click the
MyComponents\Dialog.razor
file and select the “Rename” option from the menu. -
Rename the file to
BweDialog.razor
. In this case,Bwe
stands for “Blazor WebAssembly by Example.”Tip: When naming components in a Razor class library, we should give them unique names to avoid ambiguous reference errors. Most organizations prefix all of their shared components with the same text. For example, a company named One Stop Designs (OSD) might prefix all of their shared components with
Osd
. -
Open the
ModalDialog\Pages\Index.razor
file. -
Rename the
Dialog
element toBweDialog
. -
From the “Build” menu, select the “Build Solution” option.
-
Return to the browser.
-
Use “Ctrl + R” to refresh the browser.
-
Click the “Show Dialog” button.
Test yourself
We have followed all the steps above, and the resulting project is given as follows. Press the “Run” button to execute it:
Get hands-on with 1400+ tech skills courses.