!2386 doc(#I4SLTQ): update document of Ajax component

* test: 更新 Ajax 组件单元测试
* doc: 更新 ajax 示例
* refactor: 格式化代码
* refactor: 格式化文档
This commit is contained in:
Argo 2022-02-04 05:45:40 +00:00
parent c0e4c410aa
commit 0093f60492
4 changed files with 17 additions and 24 deletions

View File

@ -14,12 +14,11 @@
<div class="mb-3">@ResultMessage</div>
<Button OnClick="Success">登录成功</Button>
<Button OnClick="Fail">登录失败</Button>
<Ajax></Ajax>
</DemoBlock>
<DemoBlock Introduction="用于Js实现页面跳转解决了Blazor页面作为SPA跳转时不会真正刷新页面的问题" Title="页面跳转" Name="Normal">
<DemoBlock Introduction="用 <code>Js</code> 实现页面跳转,解决了 <code>Blazor</code> 页面作为 <b>SPA</b> 跳转时不会真正刷新页面的问题" Title="页面跳转" Name="Goto">
<Button OnClick="Goto">跳转到文档首页</Button>
<Button OnClick="GotoSelf">跳转到自己(刷新页面)</Button>
<Ajax></Ajax>
<Button OnClick="GotoSelf">跳转到自己(刷新页面)</Button>
</DemoBlock>
<Ajax></Ajax>

View File

@ -3,7 +3,6 @@
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace BootstrapBlazor.Components;
@ -28,13 +27,13 @@ public class Ajax : BootstrapComponentBase, IDisposable
private async Task<string?> GetMessage(AjaxOption option)
{
var obj = await JSRuntime.InvokeAsync<string?>(identifier: "$.bb_ajax", option.Url, option.Method, option.Data);
var obj = await JSRuntime.InvokeAsync<string?>(null, "bb_ajax", option.Url, option.Method, option.Data);
return obj;
}
private async Task Goto(string url)
{
await JSRuntime.InvokeVoidAsync(identifier: "$.bb_ajax_goto", url);
await JSRuntime.InvokeVoidAsync(null, "bb_ajax_goto", url);
}
/// <summary>

View File

@ -16,13 +16,6 @@ public class AjaxService
/// </summary>
private List<(IComponent Key, Func<AjaxOption, Task<string?>> Callback)> Cache { get; } = new();
/// <summary>
/// 注册服务
/// </summary>
/// <param name="key"></param>
/// <param name="callback"></param>
internal void Register(IComponent key, Func<AjaxOption, Task<string?>> callback) => Cache.Add((key, callback));
/// <summary>
/// 获得 跳转其他页面的回调委托缓存集合
/// </summary>
@ -33,7 +26,7 @@ public class AjaxService
/// </summary>
/// <param name="key"></param>
/// <param name="callback"></param>
internal void RegisterGoto(IComponent key, Func<string, Task> callback) => GotoCache.Add((key, callback));
internal void Register(IComponent key, Func<AjaxOption, Task<string?>> callback) => Cache.Add((key, callback));
/// <summary>
/// 注销事件
@ -47,6 +40,13 @@ public class AjaxService
}
}
/// <summary>
/// 注册服务
/// </summary>
/// <param name="key"></param>
/// <param name="callback"></param>
internal void RegisterGoto(IComponent key, Func<string, Task> callback) => GotoCache.Add((key, callback));
/// <summary>
/// 注销事件
/// </summary>
@ -71,16 +71,10 @@ public class AjaxService
}
/// <summary>
/// 调用Goto方法跳转其他页面
/// 调用 Goto 方法跳转其他页面
/// </summary>
/// <param name="url"></param>
public async
/// <summary>
/// 调用Goto方法跳转其他页面
/// </summary>
/// <param name="url"></param>
Task
Goto(string url)
public async Task Goto(string url)
{
var cb = GotoCache.FirstOrDefault().Callback;
if (cb != null)

View File

@ -22,5 +22,6 @@ public class AjaxTest : BootstrapBlazorTestBase
_ = Context.RenderComponent<Ajax>();
_ = service.GetMessage(option);
_ = service.Goto("http://www.blazor.zone");
}
}