Easier JavaScript Interoperability in Blazor with Anonymous Types

栏目: IT技术 · 发布时间: 4年前

内容简介:Often in JavaScript, you’ll come across a function that requires or supports an options object as an argument.A good example of this is theNotice how we create and pass a new object with all of the options that the function needs to do its job as its prima

Often in JavaScript, you’ll come across a function that requires or supports an options object as an argument.

A good example of this is the SweetAlert library, which you typically call or invoke like so to prompt the user with a beautiful interactive alert on your page:

swal({
    title: "Good job!",
    text: "You clicked the button!",
    icon: "success",
});

Notice how we create and pass a new object with all of the options that the function needs to do its job as its primary argument.

This is usually cleaner and easier than sending a list of unmemorable arguments in the correct order.

In Blazor, however, when we perform calls to our JavaScript functions or those of packages that we’ve included, the default way to invoke functions is through a simple list of arguments:

JSRuntime.InvokeVoidAsync("alert", "This is an alert!");

In this example, we are calling the web browser’s alert function to prompt the user with a message through Blazor’s IJSRuntime abstraction for interoperating between C# and JavaScript.

It works, but it’s not a very sophisticated alert.

What if we wanted to use the nicer SweetAlert library instead?

This is where I’ve found that we can leverage a useful feature of the C# language to do it as effortlessly as we do in JavaScript: Anonymous Types .

Once we’ve included the SweetAlert library in our web application — either by a simple <script> tag in our _Host.cshtml file, by importing it in our JavaScript bundle, or any other method — we can invoke it from our Blazor component:

await JSRuntime.InvokeAsync<bool>("swal", new {
    title = "Good job!",
    text = "You clicked the button!",
    icon = "success"
});

With an anonymous type, we don’t have to bother with creating a new class for a single JavaScript function argument and then handling that new type in our interoperability layer.

The anonymous type is serialized to JSON and then parsed as an object argument to the JavaScript function being invoked in the browser.

We can simply declare a new object almost as easily as we can do it in JavaScript!


以上所述就是小编给大家介绍的《Easier JavaScript Interoperability in Blazor with Anonymous Types》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大数据预测

大数据预测

【美】埃里克·西格尔 / 周昕 / 中信出版社 / 2014-3 / 58.00

360公司董事长周鸿祎、《罗辑思维》主讲人罗振宇郑重推荐 2020年的一天,在你驱车前往公司的路上,导航系统通过预测交通流量,会自动帮你选择一条最合适的交通路线;车内推荐系统会根据你的饮食习惯预测你可能会喜欢吃什么,并推荐沿途的早餐店;你的电子社交助理已经为你自动选择了你可能感兴趣的社交网信息;当车内系统预测到你驾车有些分心时,座椅会自动震动进行提醒…… 以上这些情景不是科幻大片独有的......一起来看看 《大数据预测》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试