Skip to content Skip to sidebar Skip to footer

Blazor - Call Javascript From C# Class

Here is what I want to achieve: I have a (normal) class in a Blazor WASM project. I want to invoke a JavaScript function from my class. If I want to do this from a Razor component

Solution 1:

When you use DI you have to follow it through.
In general that means avoiding new, as in:

InvokeJavaScript ij = new InvokeJavaScript();  // no parameter
  • register the InvokeJavaScript as a Service in Startup.
  • inject it where you need it.

Program.cs

builder.Services.AddTransient<InvokeJavaScript>();

SomeComponent.razor

@inject InvokeJavaScript ij 

Post a Comment for "Blazor - Call Javascript From C# Class"