Reputation: 33691
With a normal single page razor component in Blazor. I can inject IJSRuntime
like this at the top of the page:
@inject IJSRuntime JSRuntime
If I create a code behind .razor.cs file for the component, how do I inject something like IJSRuntime
into the code behind file?
Upvotes: 45
Views: 26743
Reputation: 33691
In the code behind razor.cs file, IJSRunTime
or others can be injected with the [Inject]
attribute
public partial class BillingDashboard
{
[Inject]
IJSRuntime JSRuntime { get; set; }
protected override async Task MyFunction()
{
await JSRuntime.InvokeVoidAsync("console.log('test')");
}
}
Upvotes: 59