【问题标题】:JS in blazor componentBlazor 组件中的 JS
【发布时间】:2020-11-22 08:48:18
【问题描述】:

我正在尝试在 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>
    

【问题讨论】:

标签: javascript c# asp.net-core blazor


【解决方案1】:
@page "/"

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

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

_Host.cshtml 文件中的&lt;script src="_framework/blazor.server.js"&gt;&lt;/script&gt; 下放置以下脚本标记,如下所示:

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

【讨论】:

    猜你喜欢
    • 2022-08-15
    • 1970-01-01
    • 2021-08-22
    • 2023-03-16
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2021-10-03
    相关资源
    最近更新 更多