Blazor 类库,仅限内部组件

huangapple go评论97阅读模式
英文:

Blazor class library, internal-only components

问题

我正在创建一个 Blazor 类库,其中包含一些 Blazor 组件,这些组件将被其他项目引用。

然而,我的类库还包含一些我正在用作辅助功能的 Blazor 组件,我希望这些组件不能被引用我的类库的项目使用。

是否有一种方法可以防止引用我的库的项目使用这些组件?

英文:

I am creating a Blazor class library that has some Blazor components that will be referenced by other projects.

However, my class library also contains some Blazor components that I am using as helpers and would prefer it for them not to be able to be used by projects that reference my class library.

Is there a way I can prevent projects that reference my library from being able to use these components?

答案1

得分: 1

抱歉,无法覆盖 Razor 编译器生成的公共类。在 .razor 文件中使用时,Razor 编译器会生成一个公共类。

虽然你可以在 .cs 文件中创建一个内部组件,并使用类似 builder.OpenElement(0, "h1"); 的代码,但当在其他组件中使用时,Razor 编译器将不会识别这些组件。

我建议在我的库中使用一个特殊的文件夹(命名空间),以便这些“内部”组件不会自动暴露给消费者代码,同时也包含我的“主要”组件。

英文:

Unfortunately, No.

When you use a .razor file then the Razor compiler generates a public class. You cannot override that.

And while you can create an internal component in a .cs file, and use code like builder.OpenElement(0, "h1");, the Razor compiler will not recognize those when used in other components.

I would use a special folder (namespace) in my library so that those 'internal' components are not automatically exposed in consumer code along with my 'main' components.

答案2

得分: 1

如@HH所述,您不能使用Razor组件来实现它。

以下是一个简单示例,展示了如何使用C#类和RenderTreeBuilder代码来实现:

这是一个库项目中的代码。

namespace Library;

internal class InternalComponent : ComponentBase
{
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, "h1");
        builder.AddMarkupContent(1, $"Hello Blazor at {DateTime.Now.ToLongTimeString()} ");
        builder.CloseElement();
    }
}

public class ExternalComponent : ComponentBase
{
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenComponent<InternalComponent>(0);
        builder.CloseComponent();
    }
}

以及主项目中的演示Index页面:

@page "/"

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<Library.ExternalComponent />

有关使用RenderTreeBuilder构建组件的更多信息,请参阅这篇微软文章:https://learn.microsoft.com/en-us/aspnet/core/blazor/advanced-scenarios?view=aspnetcore-7.0

英文:

As @HH states, you can't do it using Razor components.

For the record, here's a simple example showing how you can using C# classes and RenderTreeBuilder code.

It's assembler for components - not for the faint hearted coder.

This is the code in a library project.

namespace Library;

internal class InternalComponent : ComponentBase
{
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, &quot;h1&quot;);
        builder.AddMarkupContent(1, $&quot;Hello Blazor at {DateTime.Now.ToLongTimeString()} &quot;);
        builder.CloseElement();
    }
}

public class ExternalComponent : ComponentBase
{
    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenComponent &lt;InternalComponent&gt;(0);
        builder.CloseComponent();
    }
}

And the demo Index page in the main project:

@page &quot;/&quot;

&lt;PageTitle&gt;Index&lt;/PageTitle&gt;

&lt;h1&gt;Hello, world!&lt;/h1&gt;

Welcome to your new app.

&lt;Library.ExternalComponent /&gt;

See this MS article for more information on builder components using the RenderTreeBuilder - https://learn.microsoft.com/en-us/aspnet/core/blazor/advanced-scenarios?view=aspnetcore-7.0

huangapple
  • 本文由 发表于 2023年7月3日 03:07:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定