如何使用Blazor服务器端应用程序加载JavaScript库

时间:2020-06-28 02:15:17

标签: blazor blazor-server-side

我有一个现有的 html 主题,其中包含一些 JavaScript ,我想将它们合并到 Blazor服务器端应用程序中。

为了合并现有的 html 主题,我在_Host.cshtml文件上添加了 JavaScript 标记,并复制了所有主题 html 标记。 Index.razor文件并将所有cssjs库文件放在wwwroot文件夹下。

只要注释掉<script src="_framework/blazor.server.js"></script>文件中的
_Host.cshtml行。但是,如果不加注释,则某些 JavaScript 库不会加载。是否将<script>标签放在<head>边还是<body>边还是<script src="_framework/blazor.server.js"></script>行的前面还是后面都没关系。

是否可以加载 JavaScript 库?

_Host.cshtml文件的外观如下:

        @page "/"
        @namespace TPAGE.Pages
        @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
        @{
            Layout = null;
        }

        <!DOCTYPE html>
        <html lang="en">
        <head>
          <meta charset="utf-8">
          <title>Test Page</title>
          <meta content="width=device-width, initial-scale=1.0" name="viewport">

          <!-- Favicon -->
          <link href="img/favicon.png" rel="icon">

          <!-- Google Fonts -->
          <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,700,700i|Montserrat:300,400,500,700" rel="stylesheet">

          <!-- Bootstrap CSS File -->
          <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet">

          <!-- Libraries CSS Files -->
          <link href="lib/font-awesome/css/font-awesome.min.css" rel="stylesheet">
          <link href="lib/animate/animate.min.css" rel="stylesheet">
          <link href="lib/ionicons/css/ionicons.min.css" rel="stylesheet">
          <link href="lib/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet">
          <link href="lib/lightbox/css/lightbox.min.css" rel="stylesheet">

          <!-- Main Stylesheet File -->
          <link href="css/style.css" rel="stylesheet">     
            
        </head>
        <body>
            
            <app>
                <component type="typeof(App)" render-mode="ServerPrerendered" />
            </app>

            <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>

            <!-- JavaScript Libraries -->
            <script src="lib/jquery/jquery.min.js"></script> 
            <script src="lib/jquery/jquery-migrate.min.js"></script>
            <script src="lib/bootstrap/js/bootstrap.bundle.min.js"></script>
            <script src="lib/easing/easing.min.js"></script>
            <script src="lib/mobile-nav/mobile-nav.js"></script>
            <script src="lib/wow/wow.min.js"></script>
            <script src="lib/waypoints/waypoints.min.js"></script>
            <script src="lib/counterup/counterup.min.js"></script>
            <script src="lib/owlcarousel/owl.carousel.min.js"></script>
            <script src="lib/isotope/isotope.pkgd.min.js"></script>
            <script src="lib/lightbox/js/lightbox.min.js"></script>
            <!-- Contact Form JavaScript File -->
            <script src="contactform/contactform.js"></script>

            <!-- Template Main Javascript File -->
            <script src="js/main.js"></script>
            
        </body>
        </html>

1 个答案:

答案 0 :(得分:0)

通过从<body>文件的_Host.cshtml标记中删除所有这些行,使它起作用。

            <app>
                <component type="typeof(App)" render-mode="ServerPrerendered" />
            </app>

            <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>

然后在该位置添加了 HTML 主题标签。因此<body>看起来像这样:

<body>

    <PASTE ALL THE THEME HTML TAGS GO HERE>

    <script src="_framework/blazor.server.js"></script>

    <!-- JavaScript Libraries -->
    <script src="theme/lib/jquery/jquery.min.js"></script>
    <script src="theme/lib/jquery/jquery-migrate.min.js"></script>
    <script src="theme/lib/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="theme/lib/easing/easing.min.js"></script>
    <script src="theme/lib/mobile-nav/mobile-nav.js"></script>
    <script src="theme/lib/wow/wow.min.js"></script> 
    <script src="theme/lib/waypoints/waypoints.min.js"></script>
    <script src="theme/lib/counterup/counterup.min.js"></script>
    <script src="theme/lib/owlcarousel/owl.carousel.min.js"></script>
    <script src="theme/lib/isotope/isotope.pkgd.min.js"></script>
    <script src="theme/lib/lightbox/js/lightbox.min.js"></script>
    <!-- Contact Form JavaScript File -->
    <script src="contactform/contactform.js"></script>

    <!-- Template Main Javascript File -->
    <script src="theme/js/main.js"></script>
</body>