Blazor Breadcrumb Service
Use Blazor Bootstrap breadcrumb service to update the breadcrumb from any page or component in your app. The examples below show how it works and how to set it up in a layout.
Use Blazor BreadcrumbService to update the breadcrumb from any page or component in your app.
How it works #
BreadcrumbService sends a new list of items to every Breadcrumb component on the page that has no Items set by its parent.
How to use:
- Add a
Breadcrumbcomponent without theItemsparameter, usually in a layout. - Inject
BreadcrumbServicein the page or component that knows the current location. - Call
Notify(...)with aList<BreadcrumbItem>. SetIsCurrentPageon the last item.
<Breadcrumb />
<div class="d-flex flex-wrap gap-2 mt-3">
<Button Color="ButtonColor.Primary" @onclick="ShowOrders">Orders</Button>
<Button Color="ButtonColor.Primary" @onclick="ShowOrder">Order 1001</Button>
<Button Color="ButtonColor.Primary" @onclick="ShowSettings">Settings</Button>
</div>
@code {
[Inject] BreadcrumbService BreadcrumbService { get; set; } = default!;
protected override void OnAfterRender(bool firstRender)
{
// The Breadcrumb subscribes when it initializes, so send the first trail after the first render.
if (firstRender)
ShowOrders();
}
private void ShowOrders()
{
BreadcrumbService.Notify(new List<BreadcrumbItem>
{
new BreadcrumbItem { Text = "Home", Href = "/" },
new BreadcrumbItem { Text = "Orders", IsCurrentPage = true }
});
}
private void ShowOrder()
{
BreadcrumbService.Notify(new List<BreadcrumbItem>
{
new BreadcrumbItem { Text = "Home", Href = "/" },
new BreadcrumbItem { Text = "Orders", Href = DemoRouteConstants.Demos_URL_BreadcrumbService },
new BreadcrumbItem { Text = "Order 1001", IsCurrentPage = true }
});
}
private void ShowSettings()
{
BreadcrumbService.Notify(new List<BreadcrumbItem>
{
new BreadcrumbItem { Text = "Home", Href = "/" },
new BreadcrumbItem { Text = "Settings", IsCurrentPage = true }
});
}
}How to setup #
1. Add the
Breadcrumb component in MainLayout.razor page as shown below.@inherits LayoutComponentBase
...
... MainLayout.razor code goes here ...
...
<Breadcrumb />
@Body2. Inject
BreadcrumbService, then call the Notify(...) method as shown below.@code {
[Inject] BreadcrumbService BreadcrumbService { get; set; } = default!;
protected override void OnInitialized()
{
BreadcrumbService.Notify(new List<BreadcrumbItem>
{
new BreadcrumbItem { Text = "Home", Href = "/" },
new BreadcrumbItem { Text = "Orders", IsCurrentPage = true }
});
base.OnInitialized();
}
}