blazor在program.cs Main中注入IJSRuntime



是否可以在program.cs中注入IJSRuntime到Main方法?

谢谢

是的,您可以从services容器

获取它。
public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");
    builder.Services.AddSingleton(new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

    var host = builder.Build();
    var jsRuntime = host.Services.GetRequiredService<IJSRuntime>(); // get the service from the DI container
    // do something like get the culture - that's what the MS example for that does
    var cultureName = await jsRuntime.InvokeAsync<string>("blazorCulture.get");
    await host.RunAsync();
}

相关内容

  • 没有找到相关文章

最新更新