Angular 生产版本无法加载 - 由于不允许的 MIME 类型(“text/html”),加载模块被阻止

kWi*_*Win 4 angular-cli angular ng-build angular8

我运行 ng build --configuration=production 构建了我的应用程序并将其上传(通过 FTP 手动)到服务器(我尝试了两个不同的)。访问像 myurl.com 这样的根目录工作正常,但每当我想直接访问子页面(例如 myurl.com/subpage)时,页面都是空白的,并且出现以下错误:\nLoading module from \xe2\x80\x9chttps://myurl.com/runtime-es2015.js\xe2\x80\x9d was blocked because of a disallowed MIME type (\xe2\x80\x9ctext/html\xe2\x80\x9d).对于 polyfills-es2015 也出现同样的错误。 js, main-es2015.js

\n\n

如果我访问根目录并导航到子页面,一切正常。

\n\n

直到现在我尝试:

\n\n
    \n
  • <base href=".">在index.html中设置

  • \n
  • "target": "es5"在 tsconfig.json 中设置

  • \n
  • 手动将脚本类型从 更改type="module"type="text/javascript"\n,如下所示:<script src="runtime-es2015.js" type="text/javascript">

  • \n
\n\n

Angular CLI 和 Angular 版本8.1

\n\n

打字稿版本3.4.5

\n\n

网页包版本4.35.2

\n\n

在服务器上,我还有一个需要的 .htaccess 文件,否则访问 myurl.com/subpage 会导致 404

\n\n
<IfModule mod_rewrite.c>\nRewriteEngine On\nRewriteBase /\nRewriteRule ^index\\.html$ - [L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.html [L]\n</IfModule>\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果有人能帮助我,我会非常感激。

\n\n

编辑\n在 ng 服务期间访问“子页面”工作正常!

\n\n

Edit2 \n在浏览器上的开发控制台中检查“网络”选项卡时,.js 文件的 GET 请求工作正常,但类型错误(html)。\n如果我加载根页面(myurl.com),则 mime-类型正确。

\n

M-x*_*M-x 6

我部署到的 Angular 应用程序也遇到了类似的问题https://host.my/app。正如其他 SO 答案中提到的,改变

<base href="/">

<base href="./">

为我解决了。只是设置为<base href=".">没有。

华泰