From d4098a6811b718e8a8473b9ca267c19d4d9906ec Mon Sep 17 00:00:00 2001 From: AiYuZhen Date: Tue, 11 Nov 2025 07:07:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20`IsAddToHead`=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E6=94=AF=E6=8C=81=E5=B0=86=20`link`?= =?UTF-8?q?=20=E6=A0=87=E7=AD=BE=E6=B7=BB=E5=8A=A0=E5=88=B0=20`head`=20?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E4=B8=AD=E3=80=82=20#7091?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/HtmlTag/Link.razor | 11 +++++---- .../Components/HtmlTag/Link.razor.cs | 23 ++++++++++++++++++- .../Components/HtmlTag/Link.razor.js | 17 ++++++++++++++ test/UnitTest/Components/LinkTest.cs | 14 ++++++++++- 4 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 src/BootstrapBlazor/Components/HtmlTag/Link.razor.js diff --git a/src/BootstrapBlazor/Components/HtmlTag/Link.razor b/src/BootstrapBlazor/Components/HtmlTag/Link.razor index 2ad08e9304c..7db63f7991a 100644 --- a/src/BootstrapBlazor/Components/HtmlTag/Link.razor +++ b/src/BootstrapBlazor/Components/HtmlTag/Link.razor @@ -1,4 +1,7 @@ -@namespace BootstrapBlazor.Components -@inherits BootstrapComponentBase - - +@namespace BootstrapBlazor.Components +@inherits BootstrapModuleComponentBase +@attribute [BootstrapModuleAutoLoader(JSObjectReference = false, AutoInvokeInit = false, AutoInvokeDispose = false)] +@if (!IsAddToHead) +{ + +} diff --git a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs index 5e2cea36217..ab1bc943ee6 100644 --- a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs +++ b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -29,8 +29,29 @@ public partial class Link [Parameter] public string? Version { get; set; } + /// + /// 是否将样式添加到 head 元素内。 + /// 同时在多个组件中使用同一个样式时可以添加到 head 中,减少 DOM 节点。 + /// + [Parameter] + public bool IsAddToHead { get; set; } = false; + [Inject, NotNull] private IVersionService? VersionService { get; set; } private string GetHref() => $"{Href}?v={Version ?? VersionService.GetVersion(Href)}"; + + /// + /// + /// + /// + protected override async Task OnAfterRenderAsync(bool firstRender) + { + await base.OnAfterRenderAsync(firstRender); + if (IsAddToHead && firstRender) + { + var obj = new { Href = GetHref(), Rel }; + await InvokeVoidAsync("init", obj); + } + } } diff --git a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js new file mode 100644 index 00000000000..0e27d8dfea6 --- /dev/null +++ b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js @@ -0,0 +1,17 @@ +export function init(options) { + const href = options.href; + const rel = options.rel; + const links = document.head.getElementsByTagName("link"); + // 遍历所有link元素 + for (let i = 0; i < links.length; i++) { + const hlink = links[i]; + var nhref = hlink.baseURI + href; + if (hlink.href == nhref && hlink.rel == rel) { + return; + } + } + const link = document.createElement('link'); + link.rel = rel; + link.href = href; + document.head.appendChild(link); +} diff --git a/test/UnitTest/Components/LinkTest.cs b/test/UnitTest/Components/LinkTest.cs index 3f42a2f9ff6..49f3c7b5de4 100644 --- a/test/UnitTest/Components/LinkTest.cs +++ b/test/UnitTest/Components/LinkTest.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -29,4 +29,16 @@ public void Link_Version() }); Assert.Equal($"", cut.Markup); } + + [Fact] + public void Link_IsAddToHead_True() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.Href, "http://www.blazor.zone"); + pb.Add(a => a.Version, "20220202"); + pb.Add(a => a.IsAddToHead, true); + }); + Assert.Equal(string.Empty, cut.Markup); + } } From d6272ac7b5d9838263f66532056f7513758a538b Mon Sep 17 00:00:00 2001 From: AiYuZhen Date: Tue, 11 Nov 2025 07:51:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E3=80=82=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/HtmlTag/Link.razor | 2 +- src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs | 4 ++-- src/BootstrapBlazor/Components/HtmlTag/Link.razor.js | 5 ++--- test/UnitTest/Components/LinkTest.cs | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/BootstrapBlazor/Components/HtmlTag/Link.razor b/src/BootstrapBlazor/Components/HtmlTag/Link.razor index 7db63f7991a..31767a7d3fb 100644 --- a/src/BootstrapBlazor/Components/HtmlTag/Link.razor +++ b/src/BootstrapBlazor/Components/HtmlTag/Link.razor @@ -1,7 +1,7 @@ @namespace BootstrapBlazor.Components @inherits BootstrapModuleComponentBase @attribute [BootstrapModuleAutoLoader(JSObjectReference = false, AutoInvokeInit = false, AutoInvokeDispose = false)] -@if (!IsAddToHead) +@if (!AddToHead) { } diff --git a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs index ab1bc943ee6..55e8ba32a3e 100644 --- a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs +++ b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.cs @@ -34,7 +34,7 @@ public partial class Link /// 同时在多个组件中使用同一个样式时可以添加到 head 中,减少 DOM 节点。 /// [Parameter] - public bool IsAddToHead { get; set; } = false; + public bool AddToHead { get; set; } = false; [Inject, NotNull] private IVersionService? VersionService { get; set; } @@ -48,7 +48,7 @@ public partial class Link protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); - if (IsAddToHead && firstRender) + if (AddToHead && firstRender) { var obj = new { Href = GetHref(), Rel }; await InvokeVoidAsync("init", obj); diff --git a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js index 0e27d8dfea6..9e81df88d2e 100644 --- a/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js +++ b/src/BootstrapBlazor/Components/HtmlTag/Link.razor.js @@ -1,11 +1,10 @@ export function init(options) { - const href = options.href; - const rel = options.rel; + const { href, rel } = options; + const nhref = new URL(href, document.baseURI); const links = document.head.getElementsByTagName("link"); // 遍历所有link元素 for (let i = 0; i < links.length; i++) { const hlink = links[i]; - var nhref = hlink.baseURI + href; if (hlink.href == nhref && hlink.rel == rel) { return; } diff --git a/test/UnitTest/Components/LinkTest.cs b/test/UnitTest/Components/LinkTest.cs index 49f3c7b5de4..a91036f63d0 100644 --- a/test/UnitTest/Components/LinkTest.cs +++ b/test/UnitTest/Components/LinkTest.cs @@ -37,7 +37,7 @@ public void Link_IsAddToHead_True() { pb.Add(a => a.Href, "http://www.blazor.zone"); pb.Add(a => a.Version, "20220202"); - pb.Add(a => a.IsAddToHead, true); + pb.Add(a => a.AddToHead, true); }); Assert.Equal(string.Empty, cut.Markup); }