When building robust and complex razor components using Blazor, interactions with the Document Object Model (DOM) become inevitable.
This package extends the JavaScript (JS) interoperability in Blazor through helpers for interacting with the DOM's Window and Element APIs.
Package Manager
Install-Package Blazor.JSInterop.Handlers -Version 1.1.1
.NET CLI
dotnet add package Blazor.JSInterop.Handlers
PackageReference
<PackageReference Include="Blazor.JSInterop.Handlers" Version="1.1.1">
To use the package, you first need to configure your dependency injection (DI) container. Add the following line of code to your Program.cs
:
builder.Services.AddBlazorInteropHandlers();
Then add references to the namespaces in your _Imports.razor
or as directives at the top of your razor component's page:
@using Blazor.JSInterop.Helpers.Services
@using Blazor.JSInterop.Helpers.Models
Finally, inject instances of the service as needed in your razor component in your directive section or code section:
// In directive section
@inject IElementHandler elementHandler
@inject IWindowHandler windowHandler
// In code section
@code
{
[Inject] IElementHandler elementHandler;
[Inject] IWindowHandler windowHandler;
}
This project is licensed under MIT. Feel free to re-use any libraries or code for non-commercial use but do your due diligence with attributing credit.