It is possible to collocate a .js file and a related .razor file as described here.
Is there a way to collocate a .js file and .cs file in a similar way? I image it looking similar to this:
MyService.cs
public class MyService
{
private readonly IJSRuntime _js;
private IJSObjectReference? _jsModule;
public MyService(IJSRuntime js)
{
_js = js;
}
public async Task MyMethod()
{
if (_jsModule == null)
{
var modulePath = $"./_content/Company.Library/Service/MyService.cs.js";
_jsModule = await _js.InvokeAsync<IJSObjectReference>("import", modulePath);
}
await _jsModule.InvokeVoidAsync("myjsmethod");
}
}
MyService.cs.js
export function myjsmethod() {
console.log('Hello World');
}
This compiles fine, but when run the MyService.cs.js file fails to load with the error “failed to fetch dynamically imported module”