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

Blazor JSInvokable without static function

$
0
0

I have my Blazor component with some JavaScript code. When there is a click detected by the JavaScript code, I want to call an JSInvokable function in my Blazor component to update the UI.

So, I created a function like this

[JSInvokable]public static async Task ChangeTab(string val){    Console.WriteLine(val);}

in the JavaScript, I added the following line:

DotNet.invokeMethodAsync('myComponent', 'ChangeTab', tabText);

This code is working and the function ChangeTab receives the value I expected. The problem is that this function is static. So, I can't change the variables. I tried to change the code like this (ActivatePage is a function in the component)

[JSInvokable("ChangeTab")]public async Task ChangeTab(string val){    ActivatePage(val);}

but in this case I get an error because the function is not static.

Error: System.ArgumentException: The assembly 'PSC.Blazor.Components.ScrollTabs' does not contain a public invokable method with [JSInvokableAttribute("ChangeTab")]

enter image description here

I checked the Microsoft documentation but I don't understand how to change the JSInvokable function to not be static.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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