DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Building Blazor Shared Components

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

Gunnar Peipman user avatar by
Gunnar Peipman
·
May. 27, 19 · Tutorial
Like (3)
Save
Tweet
Share
15.14K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • .NET Core 3.0 Preview 5
  • Blazor 3.0.0-preview5-19227-01 libraries
  • Blazor Tools 16.0.19227.1
  • Visual Studio 2019 Preview

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.

Blazor

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Apache Kafka Introduction, Installation, and Implementation Using .NET Core 6
  • What Is Browser Sandboxing?
  • Load Balancing Pattern
  • Uplevel Your Managers With Mini-M Support Groups

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: