How to use jQuery UI from Blazor component

前端 未结 4 1274
孤城傲影
孤城傲影 2020-12-20 02:51

I am working through some Blazor examples, and while trying to work with some JSInterop solutions, I ran into an issue with jQuery UI elements. I am not a proficient Javascr

4条回答
  •  孤城傲影
    2020-12-20 03:16

    The problem is that of timing: your jQuery function executes before your Blazor app has rendered.

    The way I solved this is by replacing the "onready" ($(...)) function with a named function (e.g. onBlazorReady) that I then invoke from my Blazor's MainLayout component at the right time.

    The right time being OnAfterRender.

    For example:

    MainLayout.razor:

    @code {
       [Inject]
       protected IJSRuntime JsRuntime { get; set; }
    
       protected override void OnAfterRender(bool firstRender)
       {
           if (firstRender)
               JsRuntime.InvokeVoidAsync("onBlazorReady");
       }
    }
    

    index.html:

    
    
    
        
        
        
        
        
        
        
    
    
        Loading...
    
        
        
    
    
    

    As you can see, I'm injecting IJSInterop so that I can call my onBlazorReady JS function after the LayoutComponent has rendered.

提交回复
热议问题