0
votes

I am trying to work out how to create a shared component library in Blazor. Basically i want to create my components once and then be able to use them in multiple UI projects.

Component library

I created a simple component library as follows

dotnet new blazorlib -n UsComponentLibrary.Lib

Created a component component1.razor

<h3>Component1 from lib</h3>

@code {

}

UI

I added a reference to the library in the ui project. edited the _host.chtml file to include it

@page "/"
@namespace UsComponentLibrary.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, UsComponentLibrary.Lib

Added it to one of the pages

@page "/counter"

hello

<Component1></Component1>

@code {


}

output

The only thing its displaying is Hello i am not getting the text from the component in the library. What am i missing? Its like the ui page can see the component but when I run it its not there.

1

1 Answers

2
votes

You need to import the namespaces from your library project with using statements not the addTagHelper. This was a change that came in with Preview 4.

I would suggest you edit the root _Imports.razor file in your UI project and add the namespaces there. It should look something like this:

@using UsComponentLibrary.Lib
@using UsComponentLibrary.Lib.Components
@using UsComponentLibrary.Lib.WhatEverOtherNameSpaceYouNeed

It's also worth noting that the Blazor library project will be depreciated soon and Razor class libraries will be the way to share components for Blazor apps.