diff --git a/src/Masa.Blazor/Extensions/ElementReferenceExtensions.cs b/src/Masa.Blazor/Extensions/ElementReferenceExtensions.cs index 9f429ae1b7..b8550bb5a6 100644 --- a/src/Masa.Blazor/Extensions/ElementReferenceExtensions.cs +++ b/src/Masa.Blazor/Extensions/ElementReferenceExtensions.cs @@ -4,11 +4,6 @@ namespace Masa.Blazor.Extensions; public static class ElementReferenceExtensions { - public static string? GetSelector(this ElementReference? elementReference) - { - return elementReference?.GetSelector(); - } - public static string GetSelector(this ElementReference elementReference) { if (elementReference.TryGetSelector(out var selector)) @@ -16,7 +11,8 @@ public static string GetSelector(this ElementReference elementReference) return selector; } - throw new InvalidOperationException($"ElementReference {elementReference.Id} does not have a selector."); + throw new InvalidOperationException( + "ElementReference has not been configured correctly. This issue may occur during pre-rendering; it can be ignored, or you can disable pre-rendering."); } public static bool TryGetSelector(this ElementReference elementReference, [NotNullWhen(true)] out string? selector) diff --git a/src/Masa.Blazor/Extensions/JsRuntimeExtensions.cs b/src/Masa.Blazor/Extensions/JsRuntimeExtensions.cs index 51e276e4f9..a3b2a9b3f1 100644 --- a/src/Masa.Blazor/Extensions/JsRuntimeExtensions.cs +++ b/src/Masa.Blazor/Extensions/JsRuntimeExtensions.cs @@ -35,13 +35,16 @@ public static async Task AddHtmlElementEventListener(this IJSRuntime jsRuntime, Func callback, OneOf options, EventListenerExtras? extras = null) { - await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener, - el.GetSelector(), - type, - DotNetObjectReference.Create(new Invoker(callback)), - options.Value, - extras - ); + if (el.TryGetSelector(out var selector)) + { + await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener, + selector, + type, + DotNetObjectReference.Create(new Invoker(callback)), + options.Value, + extras + ); + } } public static async Task AddHtmlElementEventListener(this IJSRuntime jsRuntime, string selector, string type, @@ -57,17 +60,21 @@ await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener, ); } - public static async Task AddHtmlElementEventListener(this IJSRuntime jsRuntime, ElementReference el, string type, + public static async Task AddHtmlElementEventListener(this IJSRuntime jsRuntime, ElementReference el, + string type, Func callback, OneOf options, EventListenerExtras? extras = null) { - await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener, - el.GetSelector(), - type, - DotNetObjectReference.Create(new Invoker(callback)), - options.Value, - extras - ); + if (el.TryGetSelector(out var selector)) + { + await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener, + selector, + type, + DotNetObjectReference.Create(new Invoker(callback)), + options.Value, + extras + ); + } } public static async Task AddClickEventListener(this IJSRuntime jsRuntime, ElementReference elementReference, Func callback, bool stopPropagation, bool preventDefault) @@ -84,12 +91,18 @@ public static async Task RemoveHtmlElementEventListener(this IJSRuntime jsRuntim public static async Task RemoveHtmlElementEventListener(this IJSRuntime jsRuntime, ElementReference el, string type, string? key = null) { - await jsRuntime.InvokeVoidAsync(JsInteropConstants.RemoveHtmlElementEventListener, el.GetSelector(), type, key); + if (el.TryGetSelector(out var selector)) + { + await jsRuntime.InvokeVoidAsync(JsInteropConstants.RemoveHtmlElementEventListener, selector, type, key); + } } public static async Task RemoveClickEventListener(this IJSRuntime jSRuntime, ElementReference el, string? key = null) { - await jSRuntime.RemoveHtmlElementEventListener(el.GetSelector(), "click", key); + if (el.TryGetSelector(out var selector)) + { + await jSRuntime.RemoveHtmlElementEventListener(selector, "click", key); + } } public static async Task ScrollTo(this IJSRuntime jsRuntime, string selector, double? top, double? left = null,