Skip to content

Commit d4098a6

Browse files
author
AiYuZhen
committed
添加 IsAddToHead 参数,支持将 link 标签添加到 head 元素中。
#7091
1 parent e7d93b0 commit d4098a6

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
@namespace BootstrapBlazor.Components
2-
@inherits BootstrapComponentBase
3-
4-
<link @attributes="AdditionalAttributes" href="@GetHref()" rel="@Rel" />
1+
@namespace BootstrapBlazor.Components
2+
@inherits BootstrapModuleComponentBase
3+
@attribute [BootstrapModuleAutoLoader(JSObjectReference = false, AutoInvokeInit = false, AutoInvokeDispose = false)]
4+
@if (!IsAddToHead)
5+
{
6+
<link @attributes="AdditionalAttributes" href="@GetHref()" rel="@Rel" />
7+
}

src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
@@ -29,8 +29,29 @@ public partial class Link
2929
[Parameter]
3030
public string? Version { get; set; }
3131

32+
/// <summary>
33+
/// 是否将样式添加到 head 元素内。
34+
/// <para>同时在多个组件中使用同一个样式时可以添加到 head 中,减少 DOM 节点。</para>
35+
/// </summary>
36+
[Parameter]
37+
public bool IsAddToHead { get; set; } = false;
38+
3239
[Inject, NotNull]
3340
private IVersionService? VersionService { get; set; }
3441

3542
private string GetHref() => $"{Href}?v={Version ?? VersionService.GetVersion(Href)}";
43+
44+
/// <summary>
45+
/// <inheritdoc/>
46+
/// </summary>
47+
/// <param name="firstRender"></param>
48+
protected override async Task OnAfterRenderAsync(bool firstRender)
49+
{
50+
await base.OnAfterRenderAsync(firstRender);
51+
if (IsAddToHead && firstRender)
52+
{
53+
var obj = new { Href = GetHref(), Rel };
54+
await InvokeVoidAsync("init", obj);
55+
}
56+
}
3657
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function init(options) {
2+
const href = options.href;
3+
const rel = options.rel;
4+
const links = document.head.getElementsByTagName("link");
5+
// 遍历所有link元素
6+
for (let i = 0; i < links.length; i++) {
7+
const hlink = links[i];
8+
var nhref = hlink.baseURI + href;
9+
if (hlink.href == nhref && hlink.rel == rel) {
10+
return;
11+
}
12+
}
13+
const link = document.createElement('link');
14+
link.rel = rel;
15+
link.href = href;
16+
document.head.appendChild(link);
17+
}

test/UnitTest/Components/LinkTest.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
@@ -29,4 +29,16 @@ public void Link_Version()
2929
});
3030
Assert.Equal($"<link href=\"http://www.blazor.zone?v=20220202\" rel=\"stylesheet\" />", cut.Markup);
3131
}
32+
33+
[Fact]
34+
public void Link_IsAddToHead_True()
35+
{
36+
var cut = Context.RenderComponent<Link>(pb =>
37+
{
38+
pb.Add(a => a.Href, "http://www.blazor.zone");
39+
pb.Add(a => a.Version, "20220202");
40+
pb.Add(a => a.IsAddToHead, true);
41+
});
42+
Assert.Equal(string.Empty, cut.Markup);
43+
}
3244
}

0 commit comments

Comments
 (0)