首页 > 编程语言 >Blazor笔记-JavaScript Interop(JS互调用)

Blazor笔记-JavaScript Interop(JS互调用)

时间:2024-03-07 15:36:36浏览次数:30  
标签:Interop JavaScript js 实例 call NET JS

更新记录

注意:非教程。纯笔记,日常查询用的。需要教程的小伙伴找几本书看看即可哈哈,有Vue基础的话非常快,概念都是通的。非工作需要不建议深入学习Blazor,深入Vue吧,用的多,哈哈。

完整目录地址:https://www.cnblogs.com/cqpanda/p/17596348.html

点击查看
2024年3月7日 发布。
2023年8月1日 迁移笔记到博客。

.NET to JavaScript(C#调用JS)

Calling JavaScript from .NET is pretty simple. There are two ways of doing that:
• Global JavaScript
• JavaScript Isolation

Global JavaScript (the old way)

to call the method from C#, we would call it using JSRuntime.

There are two different methods we can use to call JavaScript:
• InvokeVoidAsync, which calls JavaScript, but doesn’t expect a return value.
• InvokeAsync<T>, which calls JavaScript and expects a return value of type T.

实现:

@inject IJSRuntime js
async Task xxxx()
{
    await js.InvokeVoidAsync("函数名", 参数);
}

JavaScript Module Isolation

In .NET 5, we got a new way to add JavaScript using JavaScript Isolation, which is a much nicer way to call JavaScript. It doesn’t use global methods, and it doesn’t require us to refer to the JavaScript file.

This is awesome for component vendors and end users because JavaScript will be loaded when needed. It will only be loaded once (Blazor handles that for us), and we don’t need to add a reference to the JavaScript file, which makes it easier to start and use a library

需要调用的js

export function showConfirm(message) {
 return confirm(message);
}

注入JSRuntime服务

@inject IJSRuntime jsRuntime

在代码中使用

//js模块的引用
IJSObjectReference jsmodule;

//protected override async Task OnAfterRenderAsync
或者在事件中引入
private async Task<bool> ShouldDelete()
{
 //引入js模块
 jsmodule = await jsRuntime.InvokeAsync<IJSObjectReference>("import", "/_content/Components/RazorComponents/xxxxx.js");

 //调用js模块内的函数
 return await jsmodule.InvokeAsync<bool> ("showConfirm", "Are you sure?");
}

Implement the IAsyncDisposable interface and dispose of the JavaScript module when the component is destroyed:

@implements IAsyncDisposable

@code {
    ...
    public async ValueTask DisposeAsync()
    {
        if(JSModule.IsValueCreated)
        {
            await JSModule.Value.DisposeAsync();
        }
    }
}

How to differentiate between standard JavaScript and JavaScript modules?

The syntax for using standard JavaScript and JavaScript modules is different.
For example, standard JavaScript uses global variables and functions,
while JavaScript modules use the import and export statements to define dependencies and expose functionality between files.
In addition, standard JavaScript code is executed in the global scope, while JavaScript modules have their own scope and can be imported and used in other modules.

JavaScript to .NET(JS调用C#)

There are three ways of doing a callback from JavaScript to .NET code:
• A static .NET method call
• An instance method call
• A component instance method call

从JS调用C#有两种方式,一种是调用静态方法,另外一种是调用实例方法,无论那种方式,C#中能被JS调用的函数都需要标注JSInvokable属性注解。

Static .NET method call

To call a .NET function from JavaScript, we can make the function static, and we also need to add the JSInvokable attribute to the method.

语法:

DotNet.invokeMethod("程序集的名称", "静态方法名称",参数1,…,参数n);

实例:
add a function such as this in the code section of a Razor component, or inside a class:

[JSInvokable]
public static Task<int[]> ReturnArrayAsync()
{
 return Task.FromResult(new int[] { 1, 2, 3 });
}

In the JavaScript file, we can call that function using the following code:

DotNet.invokeMethodAsync('BlazorWebAssemblySample', 'ReturnArrayAsync').then(data => {
 data.push(4);
 console.log(data);
 });

The DotNet object comes from the Blazor.js or blazor.server.js file.
BlazorWebAssemblySample is the name of the assembly, and ReturnArrayAsync is the name of the static .NET function

It is also possible to specify the name of the function in the JSInvokeable attribute if we don’t want it to be the same as the method name like this:

[JSInvokable("DifferentMethodName")]

It is returned as a promise in the JavaScript file that we are waiting for, and then (using the then perator) we continue with the execution, adding a 4 to the array and then outputting the values in the console.

把JS对象传送到.NET中
JS代码:

//创建JS引用
var jsObjectReference = DotNet.createJSObjectReference(window);
//映射到.NET中的对象
DotNet.invokeMethodAsync('{ASSEMBLY NAME}', 'ReceiveWindowObject', jsObjectReference);
//释放JS引用
DotNet.disposeJSObjectReference(jsObjectReference);

C#代码:

//在C#中使用JS的对象
[JSInvokable]
public static void ReceiveWindowObject(IJSObjectReference objRef)
{
    ...
}

Instance method call

步骤:
1.把实例对象的引用先传到前端
2.用实例对象的引用来执行方法

将方法所在类的实例(可以是组件实例,可以是其他类的实例)的引用传递到JS中。

//如果是在组件中,获得组件的实例引用
DotNetObjectReference.Create(this);
//如是其他类型
var cInstance = new SomClass();
DotNetObjectReference.Create(cInstance)


//然后传给JS前端
@inject IJSRuntime jsRuntime

protected override async void OnInitialized()
{
    //把对象实例传递到JS前端
    await js.InvokeAsync(string)("DuiYinJSDeMethod",DotNetObjectReference.Create(this))
}

//需要被JS调用的方法
[JSInvokable]
public void DoSomething(object o)
{
    //将o转为需要的类型使用
}

JS前端:

var instacne;
function DuiYinJSDeMethod(o)
{
    o.invokeMethodAsync("DoSomething",instacne);
}

实例:
后端.NET代码:

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    //获得组件的实例引用
    DotNetObjectReference.Create(this);

    //实例传给前端
    await js.InvokeAsync<string>("DuiYinJSDeMethod", DotNetObjectReference.Create(this));

    base.OnAfterRenderAsync(firstRender);
}

//JS前端需要调用的方法
[JSInvokable]
public string PP(string arg)
{
    return "abc" + arg;
}

前端JS代码:

<script>
    function DuiYinJSDeMethod(instanceReference)
    {
        //参数就是.NET对象的实例
        console.log(instanceReference);
        //调用.NET的方法
        var result = instanceReference.invokeMethodAsync("PP", "123");
        //获得结果
        result.then((data)=>{
            console.log(data);
        });
    }
</script>

Implementing an existing JavaScript library

相对于用上面的2种方法来封装JS库

标签:Interop,JavaScript,js,实例,call,NET,JS
From: https://www.cnblogs.com/cqpanda/p/17596421.html

相关文章

  • 前端页面使用js模拟ping命令
    letuserIpAddress='';//创建XMLHttpRequest对象varxhr=newXMLHttpRequest();xhr.open('GET','https://api.ipify.org/?format=json');//调用第三方API获取IP地址xhr.onload=function(){if(xhr.status===......
  • JavaScript 最新动态:2024 年新功能
    前言随着Web技术的日新月异,JavaScript也在不断地吸收新的特性和技术,以满足日益复杂和多样化的开发需求。在2024年,JavaScript迎来了一系列令人瞩目的新功能,这些功能不仅提升了开发者的效率,也极大地丰富了Web应用的表现力和交互性。在接下来的内容中,我们将逐一介绍这些新......
  • 通过debug搞清楚.vue文件怎么变成.js文件
    前言我们每天写的vue代码都是写在vue文件中,但是浏览器却只认识html、css、js等文件类型。所以这个时候就需要一个工具将vue文件转换为浏览器能够认识的js文件,想必你第一时间就想到了webpack或者vite。但是webpack和vite本身是没有能力处理vue文件的,其实实际背后生效的是vue-loade......
  • javascript如何循环遍历对象
    在JavaScript中有多种循环遍历对象的方法,下面本篇文章就来给大家介绍一下使用JavaScript循环遍历对象的方法,希望对大家有所帮助。1、使用for循环for循环是js中最常用的一个循环工具,经常用于数组的循环遍历。letarr=[1,2,3];for(leti=0;i<arr.length;i++){co......
  • Js引用其他Js文件中的方法
    目前已知有两种方法,例如在A.js文件中引用B.js文件中的方法。先说第一种:B.js文件是这样的,functionhello(){console.log("Helloworld");}exports.hello=h;那么在A.js文件中可以这样引用,//varhello=require('./B.js');consthello=require('./B.js');hell......
  • 盘点一个Pandas处理json数据的实战案例
    大家好,我是Python进阶者。一、前言前几天在Python最强王者交流群【黑科技·鼓包】问了一个Pandas处理json数据的问题。问题如下:大佬们请教下一个很简单的问题,因为我半瓢水也不知道该怎么解决,问AI好像也写不好描述,麻烦帮忙看看呗?如下json用df写的话是两列,然后写df=df[row]会变一......
  • JavaScript逆向之RSA算法
    RSA算法简介RSA算法属于非对称加密,加密的密钥称为公钥,解密的密钥称为私钥,公钥和私钥不是同一个。公钥是可以放在外面的,给谁都可以;但是私钥不可以放在外面,只能服务器自己保留,如果私钥泄露了,数据安全将有极大的风险。RSA的公钥和私钥是成对的,不能拆开。python中的RSA在python中......
  • 消除js计算误差,消除使用减号“-”或加号“+”的计算误差
    做条形图统计计算时,往往js计算“-”后再无法相“+”等于100%或者说等于1,后来发现是js使用减号“-”或加号“+”计算有误差相关代码:this.standard=99.68this.nonstandard=100-this.standardconsole.info("this.nonstandard:",this.nonstandard)//进一法this.nonstandard......
  • js 时间数组如何url传参 和接收参数
    在JavaScript中,如果你想通过URL传递一个时间数组,你需要先将数组转换成字符串格式,因为URL参数只能传输字符串。有多种方式可以实现这个转换,例如使用JSON.stringify()将数组转换成JSON字符串。下面是一个示例,展示了如何将时间数组转换成URL参数,并在另一个页面接收这些参数:发送时间......
  • React jsx 语法解析 & 转换原理
    jsx介绍jsx是一种JavaScript的语法扩展(eXtension),也在很多地方称之为JavaScriptXML,因为看起就是一段XML语法,用于描述UI界面,并且可以和JavaScript代码结合使用。比起vue中的模板语法,更加灵活,且不需要学习模板语法中的特定标签,比如:v-if、v-for、v-bind等,而是直接使用JavaScript语......