我正在研究如何在Blazor中创建一个共享组件库。基本上,我想创建一次我的组件,然后能够在多个UI项目中使用它们。
组件库
我创建了一个简单的组件库,如下所示
dotnet new blazorlib -n UsComponentLibrary.Lib已创建组件component1.razor
<h3>Component1 from lib</h3>
@code {
}用户界面
我在ui项目中添加了对该库的引用。已编辑_host.chtml文件以将其包括在内
@page "/"
@namespace UsComponentLibrary.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, UsComponentLibrary.Lib将其添加到其中一个页面
@page "/counter"
hello
<Component1></Component1>
@code {
}输出
它唯一显示的是Hello I not text The text from the component in the library。我遗漏了什么?就像ui页面可以看到组件一样,但当我运行它时,它就不在那里了。
发布于 2019-07-02 00:25:57
您需要使用using语句而不是addTagHelper从库项目中导入命名空间。这是预览版4中的一个变化。
我建议您编辑UI项目中的根_Imports.razor文件,并在其中添加名称空间。它应该看起来像这样:
@using UsComponentLibrary.Lib
@using UsComponentLibrary.Lib.Components
@using UsComponentLibrary.Lib.WhatEverOtherNameSpaceYouNeed同样值得注意的是,Blazor库项目很快就会贬值,而Razor类库将成为Blazor应用程序共享组件的方式。
https://stackoverflow.com/questions/56839072
复制相似问题