3

i want to set variable (someFlag = false/true) in JS so i can disable/enable javascript function.

I would like to do it with afterStarted(options) [described in link below].

https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-6.0#javascript-initializers

The question is:

Is there any way to get the data from appsettings.json to function afterStarted(...). Either through options parameter or something else, doesn't matter.

Thanks.

  • hello, did you figure out how to do this? – Fredou Sep 22 '22 at 12:25
  • 1
    @Fredou not really, as far as I am aware there is not really way to do it "clean". We have solved the issue with some work-around, we actually needed it just to disable some JS function while debuging (to save time). – Tomáš Zálešák Sep 27 '22 at 10:03

1 Answers1

2

Maybe inject IConfiguration into the blazor page?

@inject IConfiguration _configuration


protected override afterStarted(){
    var somevariable = _configuration["othervariable"];
}

Failing that, I create an object like ApplicationState with an interface, make that a scoped service in Program.cs, define the object's properties, then inject that interface into your blazor page:

Program.cs

builder.Services.AddScoped<IApplicationState, ApplicationState>();
var applicationState = app.Services.GetRequiredService<IApplicationState>();
applicationState.someVar = builder.Configuration["SomeVar"];

blazor

@inject IApplicationState _applicationState

protected override afterStarted(){
    var somevariable = _applicationState.someVar;
}

Hope this helps

Liam Kenny
  • 129
  • 1
  • 11
  • 1
    Hey, as work-around this would work, but my intend is to use, if possible, blazors supported (?) javascript module, as described in theirs documentation. From what I can see its somewhat new (since .NET 6), so resources are scarce... – Tomáš Zálešák May 25 '22 at 08:22
  • I have re-read the question and now I understand what you are trying to achieve. Unfortunately I cannot help any further on this one :( – Liam Kenny May 25 '22 at 08:46
  • But, if you want to pass the variable from appsettings into js, you could just do it in the JSInterop way, pass the variable like the example above, then call the js function from the C# code I guess – Liam Kenny May 25 '22 at 08:48