我创建了一个 Blazor 服务器端应用程序。然后将自定义组件添加到 MarkupString 内。 MarkupString 仅将声明的字符串转换为 HTML 标记。但它不会将自定义组件转换为 HTML 元素。 Blazor 平台是否支持此功能。
Counter声明计数器组件(自定义组件):
<div style="height:100%; width:100%;">
@((MarkupString)@Markup)
</div>
@code {
string Markup = "<Counter></Counter>";
}
Counter.razor 页面包含以下代码。
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
预期输出:
我期望执行计数器组件并在 DOM 中渲染输出元素。
最佳答案
Can MarkupString property convert a string of custom component into HTML elements in DOM
不,不能。
MarkupString 允许您呈现原始 HTML。它将 MarkupString 内容解析为 HTML 或 SVG,然后将其插入到 DOM 中。但是,它不会渲染您的 Counter 组件。相反,它将标签转换为小写,并将其视为 HTML 标签。这是结果:
<div style="height:100%; width:100%;">
<counter></counter>
</div>
Is this supported in Blazor platform.
如您所见,事实并非如此。然而,这在未来可能是可能的,也许是由 Blazor 社区实现的。我对此不确定,但我认为史蒂夫·桑德森与此有关......
希望这有帮助...
关于c# - MarkupString属性能否将自定义组件的字符串转换为DOM中的HTML元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62026281/