我在 blazor 应用程序的其中一个页面中实现了一个文件放置区域,该区域是使用 javascript 运行时接口进行处理的。为了避免内存泄漏,我有一个 javascript 方法,它会删除所有事件侦听器。这是从 dispose 函数中调用的,如下所示:
public ValueTask DisposeAsync()
{
ViewModel.PropertyChanged -= OnPropertyChangedHandler;
GC.SuppressFinalize(this);
return JSRuntime.InvokeVoidAsync("deInitializeDropZone");
}
Run Code Online (Sandbox Code Playgroud)
这是可行的,但是如果我在浏览器中重新加载页面(F5 或重新加载按钮),则会出现以下异常:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMI59N5RRGP7", Request id "0HMI59N5RRGP7:0000000E": An unhandled exception was thrown by the application.
System.InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime.BeginInvokeJS(Int64 asyncHandle, String identifier, String argsJson, …Run Code Online (Sandbox Code Playgroud)