英文:
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, "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();
}
}
And the demo Index
page in the main project:
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<Library.ExternalComponent />
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论