Blazor FAQ - Component libraries

How do you share and reuse components across various Blazor projects?

Razor Class Library (RCL) can be used to share components across various projects and Blazor apps. RCL can be utilized from:

  • Another project in the solution.
  • A NuGet package.
  • A referenced .NET library.

For more information regarding RCL, check out this link.

Permalink
How do you make the Blazor library component available to the entire component?

You have to include the library at the root-level _Imports.razor file through the @using directive.

@using MyComponentLib
Permalink
How do you consume a Blazor component library?

You can consume a library in either of two ways:

  • Full name with namespace
  • Using Razor’s @using directive

If the MyComponentLib is the component library and MyComponent is the component name, those are consumed like in the following.

Option 1

<h1>Hello, world!</h1>

Welcome to your new app.

<MyComponentLib.MyComponent />

Option 2

@using MyComponent

<h1>Hello, world!</h1>

Welcome to your new app.

<MyComponent />

Refer to the link Consume Blazor a library component.

Permalink
How do you include a Blazor component library?

In Visual Studio 2019’s latest preview, create an ASP.NET Core web application and choose the Razor Class Library template with ASP.NET Core 3.0.

Refer to this Microsoft link for more information.

Permalink