skip to Main Content

I am trying to create a solution with the standard Blazor server example as one project and a class library as a second project (here it is). The purpose is I want to put a couple of my components up on NuGet for others that might find them useful.

I am struggling with how to tell Visual Studio that the class library has Blazor components. I can’t get it to understand that the component.razor.cs file is the code for component.razor.

What do I need to set to make this work?

Update: Following MrC’s example I added to the .csproj file:

<ItemGroup>
    <SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.8" />
</ItemGroup>

But still no luck. If I copy the component to the Blazor server project, it works great (see ExPopupMessageBox in example). But if I use the PopupMessageBox in the library project, I have the problems:

MainLayout.razor shows nothing for (works fine if set to ExPopupMessageBox):

<PopupMessageBox>
    <article class="content px-4">
        @Body
    </article>
</PopupMessageBox>

And the code in the library project must explicitly say it inherits ComponentBase and the PopupMessageBox.razor.cs file says it is not a partial class definition. So clearly the compiler does not see it as a component.

2

Answers


  1. Take a look at this repository of mine – https://github.com/ShaunCurtis/Blazr.EditStateTracker.

    It’s the code base for the Blazr.EditStateTracker Nuget package with the build and deploy library and a Blazor Server demo project which is I think is what you’re looking for.

    Login or Signup to reply.
  2. There is a type of project called "Razor Class Library". It might sound that it works only for Razor (MVC) pages, but I just tested it and It allows to create components that you can add from a project reference.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search