blazor 组件中的 JS

kjenn31

我正在尝试在 Blazor 组件中创建一条警报消息。我不知道该怎么做。我正在运行 ASP.NET Core 3.1 Blazor 服务器端。这是我尝试过的

组件功能:

private async Task ShowAlert()
    {
        await JSRuntime.InvokeAsync<object>("ShowMsg");
    }

Javascript互操作:

function ShowMsg() {
    success = "Success!";
    return success; 
}

文件host.cshtml

 <script src="~/BlazorInterop.js"></script>
    
@page "/"

<button @onclick="MessageBox">Show Message</button>

@code
{
    [Inject] IJSRuntime JSRuntime { get; set; }
    protected async Task MessageBox()
    {
       await JSRuntime.InvokeVoidAsync("exampleJsFunctions.ShowMsg", 
                                                    "Hello Blazor");
     }
}

把下面的脚本标签下方<script src="_framework/blazor.server.js"></script>的在_Host.cshtml文件中,就像这样:

<script src="_framework/blazor.server.js"></script>
<script>
    window.exampleJsFunctions =
    {
        ShowMsg: function (message) {
            window.alert(message);
        }
    };
</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章