简体   繁体   中英

How to call a static JavaScript method from Blazor C# code

First, my JS:

class MyClass {
    static myMethod() {
        alert("TEST");
    }
}

I have my JS runtime injected like this:

[Inject] protected IJSRuntime Js { get; set; }

I want to call a static method like this:

Js.InvokeVoidAsync("MyClass.myMethod");

but I get an error:

Microsoft.JSInterop.JSException: Could not find 'MyClass' in 'window'.

Is there a sane, reasonable way around this, or only by adding window.MyClass = MyClass to my JS?

Try this (assuming server side Blazor).

In your _Host.cshtml file:

(function () {
    window.MyClass = {
        myMethod: function () {
            return alert('TEST');
        }
    };
})();

in.razor file (at top):

@*Inject JSRuntime to allow JavaScript Interop *@
@inject IJSRuntime JSRuntime

In a method in your.razor file:

await JSRuntime.InvokeAsync<string>(
            "MyClass.myMethod", null
            );

See: Blazor JavaScript Interop

Yes, registering your method with the window object is correct.

The relevant part of the source code can be found here:

https://github.com/aspnet/Extensions/tree/master/src/JSInterop

Peeking into it you will find:

invokeJSFromDotNet: (identifier: string, argsJson: string) => { ... }

where

@param identifier Identifies the globally-reachable function to invoke.

Taking a look at the findJSFunction you see the part that checks if the given identifier is registered with the window object and if not generates the error you see:

function findJSFunction(identifier: string): Function {

(...)

    let result: any = window;
    let resultIdentifier = 'window';
    let lastSegmentValue: any;
    identifier.split('.').forEach(segment => {
      if (segment in result) {
        lastSegmentValue = result;
        result = result[segment];
        resultIdentifier += '.' + segment;
      } else {
        throw new Error(`Could not find '${segment}' in '${resultIdentifier}'.`);
      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM