Skip to content

Commit 0cae075

Browse files
authored
feat(Title): update TitleService (#4539)
* feat: 更改为服务 * refactor: 移除 Title 组件 * test: 更新单元测试 * test: 更新单元测试 * refactor: 增加 Token 代码
1 parent b21d36f commit 0cae075

File tree

6 files changed

+22
-84
lines changed

6 files changed

+22
-84
lines changed

src/BootstrapBlazor/Components/Title/Title.cs

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace BootstrapBlazor.Components;
88
/// <summary>
99
/// Title 组件
1010
/// </summary>
11-
[BootstrapModuleAutoLoader(ModuleName = "title", AutoInvokeInit = false, AutoInvokeDispose = false)]
12-
public class Title : BootstrapModuleComponentBase
11+
public class Title : ComponentBase
1312
{
1413
[Inject]
1514
[NotNull]
@@ -21,16 +20,6 @@ public class Title : BootstrapModuleComponentBase
2120
[Parameter]
2221
public string? Text { get; set; }
2322

24-
/// <summary>
25-
/// OnInitialized 方法
26-
/// </summary>
27-
protected override void OnInitialized()
28-
{
29-
base.OnInitialized();
30-
31-
TitleService.Register(this, SetTitle);
32-
}
33-
3423
/// <summary>
3524
/// <inheritdoc/>
3625
/// </summary>
@@ -42,33 +31,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4231

4332
if (firstRender && Text != null)
4433
{
45-
var op = new TitleOption() { Title = Text };
46-
await SetTitle(op);
47-
}
48-
}
49-
50-
private async Task SetTitle(TitleOption op)
51-
{
52-
if (Module != null)
53-
{
54-
await InvokeVoidAsync("setTitle", op.Title);
55-
}
56-
else
57-
{
58-
Text = op.Title;
59-
}
60-
}
61-
62-
/// <summary>
63-
/// <inheritdoc/>
64-
/// </summary>
65-
protected override async ValueTask DisposeAsync(bool disposing)
66-
{
67-
await base.DisposeAsync(disposing);
68-
69-
if (disposing)
70-
{
71-
TitleService.UnRegister(this);
34+
await TitleService.SetTitle(Text, CancellationToken.None);
7235
}
7336
}
7437
}

src/BootstrapBlazor/Components/Title/TitleOption.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/BootstrapBlazor/Components/Title/TitleService.cs renamed to src/BootstrapBlazor/Services/TitleService.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ namespace BootstrapBlazor.Components;
88
/// <summary>
99
/// Title 服务
1010
/// </summary>
11-
public class TitleService : BootstrapServiceBase<TitleOption>
11+
public class TitleService(IJSRuntime jSRuntime)
1212
{
13+
[NotNull]
14+
private JSModule? _module = null;
15+
1316
/// <summary>
1417
/// 设置当前网页 Title 方法
1518
/// </summary>
1619
/// <returns></returns>
17-
public async Task SetTitle(string title)
20+
public async Task SetTitle(string title, CancellationToken token = default)
1821
{
19-
var op = new TitleOption() { Title = title };
20-
await Invoke(op);
22+
_module ??= await jSRuntime.LoadModule("./_content/BootstrapBlazor/modules/utility.js");
23+
await _module.InvokeVoidAsync("setTitle", token, title);
2124
}
2225
}

src/BootstrapBlazor/wwwroot/modules/title.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ const deepMerge = (obj1, obj2) => {
791791
return obj1;
792792
}
793793

794+
export function setTitle(title) {
795+
document.title = title;
796+
}
797+
794798
export {
795799
autoAdd,
796800
autoRemove,

test/UnitTest/Components/TitleTest.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,14 @@ namespace UnitTest.Components;
88
public class TitleTest : BootstrapBlazorTestBase
99
{
1010
[Fact]
11-
public void ChildContent_Ok()
11+
public async Task SetTitle_Ok()
1212
{
1313
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
1414
{
15-
pb.AddChildContent<MockTitleTest>();
16-
});
17-
cut.InvokeAsync(async () =>
18-
{
19-
var titleService = cut.FindComponent<MockTitleTest>().Instance.TitleService;
20-
await titleService.SetTitle("test");
21-
});
22-
23-
cut.InvokeAsync(() =>
24-
{
25-
var title = cut.FindComponent<Title>();
26-
title.SetParametersAndRender();
27-
28-
// 模拟 Module 为空
29-
var moduleProperty = title.Instance.GetType().GetProperty("Module", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
30-
moduleProperty?.SetValue(title.Instance, null);
31-
32-
var methodInfo = title.Instance.GetType().GetMethod("SetTitle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
33-
methodInfo?.Invoke(title.Instance, new object[] { new TitleOption() { Title = "test" } });
15+
pb.AddChildContent<MockTitle>();
3416
});
17+
var mockTitle = cut.FindComponent<MockTitle>();
18+
await mockTitle.Instance.SetTitle("test");
3519
}
3620

3721
[Fact]
@@ -43,12 +27,16 @@ public void Text_Ok()
4327
});
4428
var text = cut.Instance.Text;
4529
Assert.Equal("Text", text);
30+
31+
cut.SetParametersAndRender();
4632
}
4733

48-
private class MockTitleTest : ComponentBase
34+
private class MockTitle : ComponentBase
4935
{
5036
[Inject]
5137
[NotNull]
5238
public TitleService? TitleService { get; set; }
39+
40+
public Task SetTitle(string title) => TitleService.SetTitle(title);
5341
}
5442
}

0 commit comments

Comments
 (0)