简体   繁体   English

MarkupString 属性能否将一串自定义组件转换为 DOM 中的 HTML 元素

[英]Can MarkupString property convert a string of custom component into HTML elements in DOM

I have created a Blazor server side application.我创建了一个 Blazor 服务器端应用程序。 And then add the custom component inside the MarkupString .然后在MarkupString中添加自定义组件。 MarkupString only converts the declared string as a HTML tag. MarkupString 仅将声明的字符串转换为 HTML 标记。 But it will not convert the custom component as a HTML elements.但它不会将自定义组件转换为 HTML 元素。 Is this supported in Blazor platform. Blazor 平台是否支持此功能。

Counter declares the counter component (custom component): Counter 声明了计数器组件(自定义组件):

<div style="height:100%; width:100%;">
    @((MarkupString)@Markup)
</div>
@code {
   string Markup = "<Counter></Counter>";
}

Counter.razor page contains the following code. 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>

Expected output:预期 output:

I am expecting to execute the counter component and render the output elements in DOM.我期待执行计数器组件并在 DOM 中呈现 output 元素。

Can MarkupString property convert a string of custom component into HTML elements in DOM MarkupString 属性能否将一串自定义组件转换为 DOM 中的 HTML 元素

No, it can't.不,它不能。

MarkupString allows you to render raw HTML. MarkupString 允许您渲染原始 HTML。 It parses MarkupString content as HTML or SVG and then insert it into the DOM.它将 MarkupString 内容解析为 HTML 或 SVG 然后将其插入 DOM。 However, it won't render your Counter component.但是,它不会呈现您的 Counter 组件。 Instead it converts the tag to lowercase, and treat it as an HTML tag.相反,它将标签转换为小写,并将其视为 HTML 标签。 This is the result:这是结果:

<div style="height:100%; width:100%;">
    <counter></counter>
</div>

Is this supported in Blazor platform. Blazor 平台是否支持此功能。

As you can see, it is not.如您所见,事实并非如此。 However, that might be possible in the future, perhaps by the Blazor community.但是,这可能在未来成为可能,也许是 Blazor 社区。 I'm not sure about this, but I think that Steve Sanderson had something related to this...我不确定这一点,但我认为史蒂夫桑德森与此有关......

Hope this helps...希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM