Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to inherit razor markup from a component?

I'm currently trying to abstract away a mechanism to enumerate an IAsyncEnumerable as it becomes available.

I have a base component class that looks roughly like this

// EnumerableRazorComponentBase.razor
@typeparam TObject

@if (!(this.enumerationTask?.IsCompleted ?? false))
{
    // Display loading bar
}
// EnumerableRazorComponentBase.razor.cs
public abstract partial class EnumerableRazorComponentBase<TObject> : ComponentBase
{
    private Task enumerationTask;
    // rest of enumeration logic...
}

Now I inherit this base class in a razor component

// SomeComponent.razor
@inherits EnumerableRazorComponentBase<IMyInterface>

// other razor markup

@code {
    // rest impl
}

EnumerableRazorComponentBase.razor.cs is inherited properly and works as expected, but the markup in EnumerableRazorComponentBase.razor is not added to the markup of SomeComponent.razor.

Is it possible to inherit razor markup from a base class, and if so what am I doing wrong/what am I missing?

like image 315
Izumemori Avatar asked Nov 06 '22 09:11

Izumemori


1 Answers

Yes it's possible. Just add a call to base.BuildRenderTree like so:

// SomeComponent.razor
@inherits EnumerableRazorComponentBase<IMyInterface>

@{
    base.BuildRenderTree(__builder);
}

<h2>Additional markup in subclassed component</h2>

@code {
    // rest impl
}
like image 113
Marco Avatar answered Nov 12 '22 16:11

Marco