Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building Blazor Shared Components

DZone 's Guide to

Building Blazor Shared Components

Learn how to build functionality that allows for components to be shared between Blazor apps.

· Web Dev Zone ·
Free Resource

Blazor has experimental support for shared components. Developers can build application-agnostic Blazor components and, when packed with a Blazor shared components library, these components can be shared between Blazor applications. This blog post shows how to build shared Blazor components.

Prerequisites

This blog post was written when the following prerequisites were valid:

A working demo of shared components is available in my GitHub repository gpeipman/BlazorDemo. Project OutOfBox.SharedBlazorComponent is a default sample component and OutOfBox.SharedComponentDemoApp is a client-side Blazor project demonstrating it.

Creating Blazor Shared Components

As this is not an officially supported feature yet we have to use tools that we have. The nice thing is that current unsupported tooling works pretty well. To create your first shared Blazor component, open the command line and run the following commands.

dotnet new -i Microsoft.AspNetCore.Blazor.Templates 
dotnet new blazorlib -o MySharedBlazorLibrary

The first command downloads and installs templates for the dotnet tool. Second command creates a new shared component project. Files created with shared components are shown in red rectangle on the following image.

Image title

As of writing this post, the shared components project is using Blazor 0.7.0. To upgrade it to preview5 replace contents of project file with the following markup.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <OutputType>Library</OutputType>
    <IsPackable>true</IsPackable>
    <BlazorLinkOnBuild>false</BlazorLinkOnBuild>
    <LangVersion>7.3</LangVersion>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup>
    <!-- .js/.css files will be referenced via <script>/<link> tags; other content files will just be included in the app's 'dist' directory without any tags referencing them -->
    <EmbeddedResource Include="content\**\*.js" LogicalName="blazor:js:%(RecursiveDir)%(Filename)%(Extension)" />
    <EmbeddedResource Include="content\**\*.css" LogicalName="blazor:css:%(RecursiveDir)%(Filename)%(Extension)" />
    <EmbeddedResource Include="content\**" Exclude="**\*.js;**\*.css" LogicalName="blazor:file:%(RecursiveDir)%(Filename)%(Extension)" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview5-19227-01" PrivateAssets="all" />
  </ItemGroup>

</Project>

Save the project file and remove the exampleJsInterop.js and ExampleJsInterop.cs files as the default component doesn't use them. Additionally, ExampleJsInterop.cs contains some outdated code that doesn't build. As a final thing, rename Component1.cshtml to Component1.blazor. Otherwise, you get bunch of errors in a temporary Component1 class generated to the obj folder. Save all your work and rebuild the shared Blazor component project.

NB! In case of build errors where files seem to be out-dated, close all editor windows in Visual Studio, open the project folder, and remove all files from bin and obj folders. After this try building again. It may take some deleting and building until things start work.

Using Shared Blazor Components on Pages

To use a shared component in some Blazor projects you have to add the namespace of the shared component to the _ViewImports.razor file.

@using System.Net.Http
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Layouts
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.JSInterop
@using OutOfBox.ClientSideBlazor
@using OutOfBox.ClientSideBlazor.Shared

@* My shared component namespace *@
@using OutOfBox.SharedBlazorComponent 

For this post, I'm using out-of-box client-side Blazor project. This is how shared component is included on Index page.

@page "/"
<Component1 />
<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

And this is how shared Blazor component looks on Index page of test application.

Image title

Everything works and after some struggling we wrote another story with happy ending.

Wrapping Up

Although shared Blazor components are not yet part of the big game there's still something we can use. Developers can start building Blazor components they want to share between applications or even ones they want to share with the world. The way to working component was a little bit painful but let's not forget that we are playing with prerelease versions. Still result is actually good enough to start building real shared components for Blazor.

Topics:
web dev ,blazor tutorial ,c# tutorial ,blazor shared components

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}