blazorblazor-client-sideinteropservicesasp.net-blazorblazor-jsinterop

How to run bundled javascript files in blazor web assembly?


I have kept the bundled script files in wwwroot folder of blazor_wasm app.

  1. I added the file using script tags in index.html
    <script src="assets/plugins/global/plugins.bundle.js"></script>

    <script src="assets/js/scripts.bundle.js"></script>
  1. I have put scripts.bundle.js code in a function callJS

    function callJS() {/** bundled js**/}

  2. I called the function using js interops in a razor page but this is giving errors

    protected override async Task OnAfterRenderAsync(bool firstRender){
        if (firstRender)
        {
            await jsRuntime.InvokeVoidAsync("callJS");
        }
     }
    

These are the errors

The return type of an async method must be void, Task, Task, a
task-like type, IAsyncEnumerable, or IAsyncEnumerator wasm_app

'Index.OnAfterRenderAsync(bool)': return type must be 'Task' to match overridden member 'ComponentBase.OnAfterRenderAsync(bool)' wasm_app

Is this the correct way of running file or do I have to use another life cycle event?? I am using keen bootstrap theme which produces bundled js and css files using webpack.


Solution

  • The answer is simple we just have to put the js interops code in MainLayout.razor page.That is how i solved it