Invoking an static method of JS Class inside OnAfterRenderAsync in First Render throw undefined class error
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
I try to define a js class with static methods and then invoke one of its static methods through JSInterop
in Blazor WebAssembly
Razor Components inside OnAfterRenderAsync
. It give an error that the method is not exist since the class is undefined.
Example:
1- JS Class (I placed it inside Head of document )
class JSLibrary {
static _DotNetObjectReference;
static CreateInstance(instance) {
JSLibrary._DotNetObjectReference = instance;
}
static GetScores() {
var scores = _DotNetObjectReference.invokeMethod("GetScore", 0, 21);
console.log(scores);
}
static GetCredits() {
var credits = _DotNetObjectReference.invokeMethod("GetCredit");
console.log(credits);
}
static GetAverage() {
_DotNetObjectReference.invokeMethod("GetAverage");
}
}
2- Invocation of static method inside OnAfterRenderAsync
:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_jsmodule = DotNetObjectReference.Create(jSModule);// create the reference and pass to Static methods in js
await JS.InvokeVoidAsync("JSLibrary.CreateInstance",_jsmodule);
}
await base.OnAfterRenderAsync(firstRender);
}
Error:
Microsoft.JSInterop.JSException: Could not find 'JSLibrary.CreateInstance' ('JSLibrary' was undefined).
Error: Could not find 'JSLibrary.CreateInstance' ('JSLibrary' was undefined).
Expected Behavior
Expected to work as methods defined inside modules or global namespace in Java Script.
Steps To Reproduce
Create a Blazor web assembly using template and then add the class
definition to head of index.html
and put OnAfterRenderAsync
at one of the components and test it (go to url regarded to component and check the developer logs in browser to see error)
Exceptions (if any)
No response
.NET Version
6.0.201
Anything else?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Calling imported class' static method throws a TypeError error
I've already tried exporting and importing the modules like this: // Exporting the module (Bot.js) module.exports = { Bot }; // Importing the ......
Read more >Call JavaScript functions from .NET methods in ASP. ...
This article explains how to invoke JavaScript (JS) functions from .NET. For information on how to call .NET methods from JS, see Call...
Read more >Communicating between .NET and JavaScript in Blazor ...
This method will invoke the specified JavaScript function. It requires 1 parameter; A string to specify which JavaScript function to run. After ...
Read more >static - JavaScript - MDN Web Docs - Mozilla
The static keyword defines a static method or field for a class, ... Public static features are declared using the static keyword.
Read more >Pros and Cons of Using JavaScript Interop in Blazor
This blog post explains using JavaScript interop concepts in the Blazor framework, its advantages, and its limitations with code examples.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
window.JSLibrary = JSLibrary
@abdollahkahne can you create a minimal repro project as a public github repository so that we can make progress in determining if there’s something wrong here?