Is it possible to render <component>
tags inside .razor
files. I have a requirement where a static script needs to be rendered inside another Blazor component. So, I have created the script tags using BuildRenderTree
concept and tried to add the <component>
tag with render-mode
as Static
. But, it won't render in the web page.
ScriptRender.razor
public class ScriptRender : ComponentBase
{
[Parameter]
public List<string> Source { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
foreach (var scriptPath in Source)
{
builder.OpenElement(0, "script");
builder.AddAttribute(1, "src", scriptPath);
builder.CloseElement();
}
}
}
BlazorComponent.razor
<component type="typeof(ScriptRender)" render-mode="Static"></component>
// Rest of component rendering logics
I know, the <component>
tags can be used in the layout.cshtml pages. Isn't working in Blazor razor pages?
The component Tag Helper does not belong in Blazor. Blazor does not have the concept of tag helpers, but components, and thus you cannot embed it in a Blazor component. You can only use the component Tag Helper in .cshtml extension files. In the context of Blazor, the component Tag Helper is used in the _Host.cshtml file, which is actually a Razor Page that handle the initial request to a Blazor Server App, including the rendering of the Blazor app.