Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regrouping razor components in multiple subfolders under one namespace - Blazor

I have a folder of components under the Shared directory.

>Shared
    +Components
        +MyComponentType1
            -MyComponentType1Base.razor
            -MyComponentType1Title.razor
        +MyComponentType2
            -MyComponentType2Base.razor
            -MyComponentType2Title.razor
    -MainLayout.razor

To include all the elements of the components subfolders i have to write all the using statements in the _Imports.razor

@using MyApp.Shared.Components.MyComponentType1
@using MyApp.Shared.Components.MyComponentType2

So my question is : Is there a way to regroup all the components in the subfolders in the Component directory so that i'd use only one using statement?

@using MyApp.Shared.Components
like image 537
Youness Kafia Avatar asked Oct 14 '19 15:10

Youness Kafia


1 Answers

The @namespace directive was added a few versions back. You can use this to override the default namespace of your components which is the folder path by default.

        +MyComponentType1
            -MyComponentType1Base.razor
            -MyComponentType1Title.razor
        +MyComponentType2
            -MyComponentType2Base.razor
            -MyComponentType2Title.razor

MyComponentType1Title.razor

@namespace MyComponents

<span>Component implementation</span>

MyComponentType2Title.razor

@namespace MyComponents

<span>Component implementation</span>

_Imports.razor

@using MyComponents

like image 130
Ed Charbeneau Avatar answered Sep 18 '22 16:09

Ed Charbeneau