【问题标题】:Unable to use JsRuntime.InvokeAsync from OnAfterRenderAsync in Blazor Server-Side App无法在 Blazor 服务器端应用程序中使用来自 OnAfterRenderAsync 的 JsRuntime.InvokeAsync
【发布时间】:2019-10-19 18:55:31
【问题描述】:

我有一个 Blazor 应用程序,在 OnAfterRenderAsync 中,我正在调用一个需要调用 JavaScript 方法的方法 PlayVideo()。在以前版本的 Blazor 中,我能够检查 context.IsConnected 属性以确保我可以进行 JavaScript 调用。但是,在最新版本的 Blazor 中,此属性已被删除。现在,当我尝试从 PlayVideo() 方法进行 JavaScript 调用时,我收到以下错误消息:

JavaScript interop calls cannot be issued at this time. This is because the component is being prerendered and the page has not yet loaded in the browser or because the circuit is currently disconnected. Components must wrap any JavaScript interop calls in conditional logic to ensure those interop calls are not attempted during prerendering or while the client is disconnected.

根据我的阅读,从 OnAfterRenderAsync 方法调用 JavaScript 应该可以正常工作,因为它应该已连接。自从 context.IsConnected 属性被删除后,有没有办法检查我是否可以进行 JavaScript 调用?

谢谢你

【问题讨论】:

    标签: javascript c# blazor-server-side


    【解决方案1】:

    您是否检查这是在第一次渲染之后?

    我这样做的方法如下:

    protected async override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
        // call your JS here
        // await JsRuntime.InvokeVoidAsync 
        }
     }
    

    MS documentation 中描述了此行为,希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 2020-09-18
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2020-04-21
      相关资源
      最近更新 更多