Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Collocate js and cs files

$
0
0

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"


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>