如何使用Blazor使用alert(),confirm()和hint()函数? [英] How to use alert(),confirm() and prompt() function using Blazor?

查看:238
本文介绍了如何使用Blazor使用alert(),confirm()和hint()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Blazor技术.我在VS 2019中启动了一个默认的增量项目,并且我已经用Confirm()和alert修改了Decrement的代码,但是它不起作用.

I am learning the Blazor technology. I started a default increment project in VS 2019 and I have modified the code for Decrement with confirm() and alert but it does not work.

 @page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>
<button class="btn btn-primary btn-danger" onclick="if (confirm('Are you sure to Decrement')) { @DecrementCount() }">Decrement</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }

    private void DecrementCount()
    {
        currentCount--;
        // alert('Operation Successfully executed')
    }
}

在我的代码段中,verify()函数可以正常运行,但是我想调用一个Decrement函数不能正常工作,构建失败.我想在函数中添加成功消息.请提供任何选项,而不要使用Confirm(),alert()函数.

In my code snippet confirm() function works perfectly but I want to call a Decrement function is not working build failed. And I would like to add a success message in my function. Please provide any option instead of using confirm(),alert() functions.

推荐答案

不幸的是,Blazor还没有实现这样有用的功能,.
因此,您需要使用 JSRuntime 实例.

Unfortunately, there is not implementation of such useful functions in Blazor yet.
So you need to use JSRuntime instance.

@inject IJSRuntime JsRuntime;

...

@code
{
    //...
    bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure?");
    //...
}

可以在C#代码内部直接执行JS代码.这样,您可以使用任何想要创建所需行为的JS逻辑.

It makes possible to execute JS code right inside your C# code. With that you can use any JS logic you want to create behaviour you need.

请参见文档以获得详细信息.

See docs for details.

这篇关于如何使用Blazor使用alert(),confirm()和hint()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆