You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET MAUI Blazor中加载/集成外部JS文件?脚本引用标签放置位置咨询

在.NET MAUI Blazor中引用外部JS文件的正确位置

嘿,刚好我对这个场景很熟悉!在.NET MAUI Blazor项目里,你需要把外部JS的<script>引用标签放到wwwroot/index.html文件中,这就对应Blazor Server里_Layout.cshtml的作用——作为全局页面布局的入口文件。

具体操作和你之前的Blazor Server流程几乎一致:

  • 保持my.js文件放在wwwroot/js/目录下不动
  • 打开wwwroot/index.html,把<script src="js/my.js"></script>添加到合适的位置,通常建议放在</body>标签上方(这样能确保DOM加载完成后再执行JS代码)

举个例子,你的index.html结构大概会是这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>Your MAUI Blazor App</title>
    <base href="/" />
    <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
    <link href="css/app.css" rel="stylesheet" />
    <link href="[YourAppName].styles.css" rel="stylesheet" />
</head>
<body>
    <div id="app">Loading...</div>

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>
    <script src="_framework/blazor.webview.js"></script>
    <!-- 把你的JS引用放在这里 -->
    <script src="js/my.js"></script>
</body>
</html>

这样配置后,你的MAUI Blazor应用就能正常加载并使用这个外部JS文件,效果和你之前在Blazor Server里的操作完全一致~

内容的提问来源于stack exchange,提问作者nogood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:22:37