简体   繁体   English

Blazor 服务器应用程序 JavaScript 库不工作(Metronic 模板)

[英]Blazor Server App JavaScript Libraries Not working (Metronic Template)

I have Blazor Server Side app, where I have added JavaScript and CSS Libraries for Metronic Bootstrap Template.我有 Blazor 服务器端应用程序,我在其中为 Metronic Bootstrap 模板添加了 JavaScript 和 CSS 库。

The template is fully loaded when I run the app, but the JavaScript is not responding.运行应用程序时模板已完全加载,但 JavaScript 没有响应。

For example when I click on Side Menu (Arrow Icon) or Expand the Lists, or click on User Profile Drop down, all those sections are not working.例如,当我单击侧边菜单(箭头图标)或展开列表,或单击用户配置文件下拉菜单时,所有这些部分都不起作用。

But when I comment out line, Blazor Server Library ( blazor.server.js ) template starts working, but other Blazor Components don't work properly (eg Counter).但是当我注释掉行时,Blazor 服务器库( blazor.server.js )模板开始工作,但其他 Blazor 组件无法正常工作(例如计数器)。

In _Host.cshtml file Head Section added following CSS References:_Host.cshtml文件Head Section 添加以下 CSS 参考:

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700" />    
<link href="assets/plugins/custom/fullcalendar/fullcalendar.bundle.css" rel="stylesheet" type="text/css" />
<link href="assets/plugins/global/plugins.bundle.css" rel="stylesheet" type="text/css" />
<link href="assets/css/style.bundle.css" rel="stylesheet" type="text/css" />

In Body Section added following JavaScript References.Body部分中添加了 JavaScript 参考资料。

<script src="_framework/blazor.server.js"></script>
<script src="./assets/plugins/global/plugins.bundle.js"></script>
<script src="./assets/js/scripts.bundle.js"></script>
<script src="./assets/plugins/custom/fullcalendar/fullcalendar.bundle.js"></script>
<script src="./assets/js/custom/widgets.js"></script>
<script src="./assets/js/custom/apps/chat/chat.js"></script>
<script src="./assets/js/custom/modals/create-app.js"></script>
<script src="./assets/js/custom/modals/upgrade-plan.js"></script>

I also tried the following methods, but nothing is working.我也尝试了以下方法,但没有任何效果。 Looks like Blazor Server App is Rendering before these JavaScript files are loaded into DOM.看起来 Blazor 服务器应用程序在这些 JavaScript 文件加载到 DOM 之前正在渲染。 How Can I load these Scripts and work them properly.如何加载这些脚本并正常工作。 Any help will be much appriciated.任何帮助将不胜感激。

<script src="_framework/blazor.server.js" autostart="false"></script>
<script>
    Blazor.start().then(function () {
        var customScript = document.createElement('script');
        customScript.setAttribute('src', scriptURLs);
        document.head.appendChild(customScript);
    });

    const scriptURLs = [
        "./assets/plugins/global/plugins.bundle.js",
        "./assets/js/scripts.bundle.js",
        "./assets/plugins/custom/fullcalendar/fullcalendar.bundle.js",
        "./assets/js/custom/widgets.js",
        "./assets/js/custom/apps/chat/chat.js",
        "./assets/js/custom/modals/create-app.js",
        "./assets/js/custom/modals/upgrade-plan.js",
        // ...
    ];
</script>

In my MainLayout.razor在我的MainLayout.razor

@inherits LayoutComponentBase
@inject IJSRuntime JSRuntime

<div class="d-flex flex-column flex-root">
    <div class="page d-flex flex-row flex-column-fluid">
        <MainSidebar />
        <div class="wrapper d-flex flex-column flex-row-fluid" id="kt_wrapper">
            <MainHeader />
            @Body
            <MainFooter />
        </div>
    </div>
</div>

Just add the following:只需添加以下内容:

<script>
            function InitMetronicScript() {
                $.getScript('theme/assets/plugins/global/plugins.bundle.js');
                $.getScript('theme/assets/js/scripts.bundle.js');
                $.getScript('theme/assets/js/custom/widgets.js');
</script>
    

to your _Host.cshtml file到您的 _Host.cshtml 文件

and call it with并调用它

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    await JsRuntime.InvokeVoidAsync("InitMetronicScript");
}

in your Layout.razor and it should work fine.在您的 Layout.razor 中,它应该可以正常工作。

You need to start Metronic's plugin after the page is rendered and you need to compile those scripts using webpack;您需要在页面渲染后启动 Metronic 的插件,并且您需要使用 webpack 编译这些脚本; if you don't compile using webpack you will have the following error:如果您不使用 webpack 进行编译,则会出现以下错误:

Microsoft.JSInterop.JSException: Cannot set properties of undefined (setting 'moment') TypeError: Cannot set properties of undefined (setting 'moment') Microsoft.JSInterop.JSException:无法设置未定义的属性(设置“时刻”)类型错误:无法设置未定义的属性(设置“时刻”)

At the end of MainLayout.razor file you need to addMainLayout.razor文件末尾需要添加

 @inject IJSRuntime JSRuntime @code { protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await JSRuntime.InvokeAsync<IJSObjectReference>("import", "/assets/plugins/global/plugins.bundle.js"); await JSRuntime.InvokeAsync<IJSObjectReference>("import", "/assets/js/scripts.bundle.js"); } } }

And your _Host.cshtml you need to let the other Metronic's scripts, like in this snippet:而你的_Host.cshtml你需要让其他 Metronic 的脚本,就像在这个片段中一样:

 <body> <component type="typeof(App)" render-mode="ServerPrerendered" /> <div id="blazor-error-ui"> <environment include="Staging,Production"> An error has occurred. This application may no longer respond until reloaded. </environment> <environment include="Development"> An unhandled exception has occurred. See browser dev tools for details. </environment> <a href="" class="reload">Reload</a> <a class="dismiss"></a> </div> <:--begin:;Javascript--> <script>var hostUrl = "/assets/":</script> <:--begin..Page Vendors Javascript(used by this page)--> <script src="~/assets/plugins/custom/fullcalendar/fullcalendar.bundle.js"></script> <script src="~/assets/plugins/custom/datatables/datatables:bundle:js"></script> <:--end:.Page Vendors Javascript--> <.--begin..Page Custom Javascript(used by this page)--> <script src="~/assets/js/widgets.bundle.js"></script> <script src="~/assets/js/custom/widgets.js"></script> <script src="~/assets/js/custom/apps/chat/chat:js"></script> <script src="~/assets/js/custom/utilities/modals/upgrade-plan:js"></script> <script src="~/assets/js/custom/utilities/modals/users-search,js"></script> <script src="~/assets/js/custom/utilities/modals/new-target.js"></script> <.--end..Page Custom Javascript--> <div id="blazor-error-ui"> <environment include="Staging.Production"> An error has occurred. This application may no longer respond until reloaded: </environment> <environment include="Development"> An unhandled exception has occurred: See browser dev tools for details. </environment> <a href="" class="reload">Reload</a> <a class="dismiss"></a> </div> <script src="_framework/blazor.server.js"></script> <!--end::Javascript--> </body>

I got it working using metronic v8.0.38 html demo5我使用metronic v8.0.38 html demo5 让它工作了

Please note that Metronic have released a Blazor sample which does everything better than I described here: https://devs.keenthemes.com/metronic/blazor/demo1/download请注意,Metronic 发布了 Blazor 示例,它比我在这里描述的要好: https://devs.keenthemes.com/metronic/blazor/demo1/download

Cheers,干杯,

Max最大限度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM