On Dotnetconf two weeks ago I heard it is now possible to include local Javascript files in Blazor components. That sounds very interesting. I guess the approach is still to use JSIniterop and reference the module file.
BUT how do you reference the JS file ??? I have tried all possible creative variants but so far without success.
I am surprised to Google everywhere but find no guides or sample yet on this. All hints are appreciated.
You can create .js code file next to .razor.cs file and load that file in runtime. It's described here. Also I found presentation of this feature here and github repo here.
Let's say that you have ComponentA with ComponentA.razor, ComponentA.razor.cs and ComponentA.razor.js files in Pages folder. You can load this module during component's initialization like so:
[Inject]
public IJSRuntime JS { get; set; }
private IJSObjectReference module { get; set; }
protected override async Task OnInitializedAsync()
{
module = await JS.InvokeAsync<IJSObjectReference>("import", "./Pages/ComponentA.razor.js");
}
But if your component is defined in a component library project then you have to use different file path. For example if your project is named AppComponents then this path would be ./_content/AppComponents/ComponentA.razor.js.