!2382 fix(##I4RER0): update Ajax component

* 删除菜单中多余的Ajax菜单
* 添加Goto方法用于跳转
* 修复Ajax组件在失败时没有返回null,而是返回"null"字符串的BUG
This commit is contained in:
j4587698 2022-02-03 16:19:58 +00:00 committed by Argo
parent 6548b4827f
commit 3b180b956d
8 changed files with 75 additions and 7 deletions

View File

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

View File

@ -76,4 +76,14 @@ public partial class Ajaxs
}
}
}
private async Task Goto()
{
await AjaxService.Goto("/introduction");
}
private async Task GotoSelf()
{
await AjaxService.Goto("/ajaxs");
}
}

View File

@ -524,11 +524,6 @@ public sealed partial class NavMenu
Text = Localizer["Transition"],
Url = "transitions"
},
new()
{
Text = "Ajax",
Url = "ajaxs"
},
};
AddBadge(item);
}

View File

@ -23,6 +23,7 @@ public class Ajax : BootstrapComponentBase, IDisposable
{
base.OnInitialized();
AjaxService.Register(this, GetMessage);
AjaxService.RegisterGoto(this, Goto);
}
private async Task<string?> GetMessage(AjaxOption option)
@ -31,6 +32,11 @@ public class Ajax : BootstrapComponentBase, IDisposable
return obj;
}
private async Task Goto(string url)
{
await JSRuntime.InvokeVoidAsync(identifier: "$.bb_ajax_goto", url);
}
/// <summary>
/// Dispose 方法
/// </summary>
@ -39,6 +45,7 @@ public class Ajax : BootstrapComponentBase, IDisposable
if (disposing)
{
AjaxService.UnRegister(this);
AjaxService.UnRegisterGoto(this);
}
}

View File

@ -17,7 +17,13 @@
return null;
}
});
if (res == null) {
return null;
}
return JSON.stringify(res);
},
bb_ajax_goto: function (url) {
window.location.href = url;
}
});
})(jQuery);

View File

@ -23,6 +23,18 @@ public class AjaxService
/// <param name="callback"></param>
internal void Register(IComponent key, Func<AjaxOption, Task<string?>> callback) => Cache.Add((key, callback));
/// <summary>
/// 获得 跳转其他页面的回调委托缓存集合
/// </summary>
private List<(IComponent Key, Func<string, Task> Callback)> GotoCache { get; } = new();
/// <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>
@ -35,6 +47,18 @@ public class AjaxService
}
}
/// <summary>
/// 注销事件
/// </summary>
internal void UnRegisterGoto(IComponent key)
{
var item = GotoCache.FirstOrDefault(i => i.Key == key);
if (item.Key != null)
{
GotoCache.Remove(item);
}
}
/// <summary>
/// 调用Ajax方法发送请求
/// </summary>
@ -45,4 +69,23 @@ public class AjaxService
var cb = Cache.FirstOrDefault().Callback;
return cb == null ? null : await cb.Invoke(option);
}
/// <summary>
/// 调用Goto方法跳转其他页面
/// </summary>
/// <param name="url"></param>
public async
/// <summary>
/// 调用Goto方法跳转其他页面
/// </summary>
/// <param name="url"></param>
Task
Goto(string url)
{
var cb = GotoCache.FirstOrDefault().Callback;
if (cb != null)
{
await cb.Invoke(url);
}
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long