Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor can't find referenced component from other folder

Tags:

c#

blazor

I'm trying out Blazor WebAssembly, and wanted to create some new components on top of the pregenerated example project from Visual Studio.

So, essentially what I ended up is the following folder structure:

Project
\ Components
  \ Navigation
    \ BurgerMenu.razor
      BurgerMenu.razor.css
      BurgerMenu.razor.less
\ Shared
  \ MainLayout.razor
    MainLayout.razor.css
    MainLayout.razor.less

So far so good. Here are my components:

MainLayout.razor:

@using Components.Navigation;

@inherits LayoutComponentBase

<div class="sidebar">
    <BurgerMenu />
</div>

<div class="LayoutContainer">
    @Body
</div>

BurgerMenu.razor:

<div>
    Test Component
</div>

@code
{
}

As you can see, as of yet there is really nothing to write home about.

However, I can't get this to work properly. Every build complains regarding warning RZ10012: Found markup element with unexpected name 'BurgerMenu'. If this is intended to be a component, add a @using directive for its namespace.

So, I'm a bit lost now. Accoding to the official docs, the @using statement should be the proper way to import the component from a folder - Which is there. However, this still doesn't work.

If I move the BurgerMenu.razor within the /Shared folder, everything works fine.

So, what am I doing wrong?

like image 678
Sossenbinder Avatar asked Dec 17 '20 22:12

Sossenbinder


People also ask

How do you reference a component in Blazor?

Use the ref attribute for a component and define it filed with the same name and same type of component. While the Counter component is rendered, the field counter is populated along with the component instance.

Where are Razor G CS files?

The file will by default go to obj\debug\net6.

How do I get the current URL in Blazor?

Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.

How can I reuse Blazor components from another project?

You can easily create a class library project that includes Blazor components to reuse them from other projects. Those libraries are called "Razor Class Library", and you can also package it as a NuGet package. The "Razor Class Library" NuGet package can also include static web assets such as JavaScript, CSS, graphics, fonts, etc.

Why can't I run Blazor Server App in Blazor?

The reason for the problem was the "ASPNETCORE_ENVIRONMENT" environment variable. In the Blazor Server app project broken version, the configuration of the "ASPNETCORE_ENVIRONMENT" environment variable was set with "Release", not "Development".

How do I create a modular system in Blazor?

Having a modular system in blazor is … amazingly easy. You just have to tell the Router component, to look for other assemblies also I have a simple blazor app, that comes in with the template in Visual Studio. I added a Razor class type of project and in it I defined 2 files:

How does Blazor read the staticwebassets file?

The .StaticWebAssets.xml file is read by ASP.NET Core StaticWebAssetsFileProvider file provider (in Blazor Server, or Blazor WebAssembly hosted ASP.NET Core scenario) or Blazor DevServer (in Blazor WebAssembly scenario).


Video Answer


2 Answers

I'd have thought the using statement would need to include your project name:

@using Project.Components.Navigation;

If I try something similar to you the editor underlines the using statement (without the project name) in red.

like image 163
Neil W Avatar answered Oct 16 '22 11:10

Neil W


This problem was fixed for my after restarting Visual Studio for me. I'm running Professional 2019 and apparently the intellesense gets stuck.

like image 30
Negatar Avatar answered Oct 16 '22 10:10

Negatar