Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions src/BootstrapBlazor/Components/Ajax/Ajax.cs

This file was deleted.

86 changes: 0 additions & 86 deletions src/BootstrapBlazor/Components/Ajax/AjaxService.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ private static RenderFragment RenderComponents() => builder =>
builder.OpenComponent<Dialog>(0);
builder.CloseComponent();

builder.OpenComponent<Ajax>(1);
builder.CloseComponent();

builder.OpenComponent<SweetAlert>(2);
builder.CloseComponent();

Expand Down
39 changes: 39 additions & 0 deletions src/BootstrapBlazor/Services/AjaxService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using System.Text.Json;

namespace BootstrapBlazor.Components;

/// <summary>
/// Ajax 服务类
/// </summary>
public class AjaxService(IJSRuntime jSRuntime)
{
[NotNull]
private JSModule? _module = null;

private Task<JSModule> LoadModule() => jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/ajax.js");

/// <summary>
/// 调用Ajax方法发送请求
/// </summary>
public async Task<JsonDocument?> InvokeAsync(AjaxOption option, CancellationToken token = default)
{
_module ??= await LoadModule();
return await _module.InvokeAsync<JsonDocument?>("execute", token, option);
}

/// <summary>
/// 调用 Goto 方法跳转其他页面
/// </summary>
/// <param name="url"></param>
/// <param name="token"></param>
public async Task Goto(string url, CancellationToken token = default)
{
_module ??= await LoadModule();
await _module.InvokeVoidAsync("goto", token, url);
}
}
7 changes: 5 additions & 2 deletions test/UnitTest/Components/AjaxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ public async Task Ajax_Test()

var service = Context.Services.GetRequiredService<AjaxService>();
await service.InvokeAsync(option);
}

Context.RenderComponent<Ajax>();
await service.InvokeAsync(option);
[Fact]
public async Task Goto_Test()
{
var service = Context.Services.GetRequiredService<AjaxService>();
await service.Goto("http://www.blazor.zone");
}
}
Loading