3

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.

Shruti
  • 109
  • 9

1 Answers1

3

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

Shruti
  • 109
  • 9
  • 2
    Can you show this with an example that how did you achieve that? I also need to execute a static asset .js file similarly. – aquariens Sep 03 '20 at 20:12
  • 1
    Copy your bundled js folder and paste in www.root folderin blazor wasm. Because Js need interops to run so I added this function on top of --> scripts.bundle.js file window.callBundleJS = () => {****code****} and call it in Mainlayout.razor page using OnAfterRenderAsync Life cycle method. The way I have done and also include that bundled js file in index.html – Shruti Sep 05 '20 at 05:34