【发布时间】:2020-05-12 12:20:08
【问题描述】:
我有一个项目,其中用户界面基于 Angular 8,后端是 springboot java 服务。整个项目是一个多模块项目,角度部分是一个单独的模块,front-end builder 用于将角度代码构建到单个可执行 jar 中。使用嵌入式 tomcat 时应用程序运行良好。我有一个新要求,要尝试在外部 tomcat 上单独部署 angular ui 部分。但是当我将 dist 文件夹复制到 webapps 文件夹并尝试提供它时,浏览器会阻止请求说:
Loading module from “http://localhost:8080/polyfills-es2015.js” was blocked because of a disallowed MIME type (“text/html”).
在进行了一些谷歌搜索后,我了解到问题的发生是因为 Angular 8 cli 无法将 type 属性添加到 script 中的 script 标记@ 。当我手动添加类型时,一切正常。任何人都可以帮助我了解为什么会发生这种情况,以及除了手动编辑之外的可能解决问题的方法。
生成index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Application</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<i class="fas fa-chart-area"></i>
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body style="margin: 0;">
<app-root></app-root>
<script src="runtime-es2015.js" type="module"></script><script src="runtime-es5.js" nomodule defer></script><script src="polyfills-es5.js" nomodule defer></script><script src="polyfills-es2015.js" type="module"></script><script src="styles-es2015.js" type="module"></script><script src="styles-es5.js" nomodule defer></script><script src="scripts.js" defer></script><script src="vendor-es2015.js" type="module"></script><script src="vendor-es5.js" nomodule defer></script><script src="main-es2015.js" type="module"></script><script src="main-es5.js" nomodule defer></script></body>
</html>
所以,总结一下所有可行的方法,如下:
- 在 HTML5 中 type 属性不再是强制性的,因此 Angular cli 不再将其添加为属性。在嵌入式 tomcat 中,资产被复制到
ROOT完美工作,当我部署在外部 tomcat 中时,我将资产保存在 webapps 中的文件夹下,这意味着我必须修改baseHref字段(在构建期间使用命令或手动构建后)以反映相同。 以下作品: - 将资产保存在 webapps 中的 ROOT 文件夹下(一切正常,因为 js 文件现在位于根目录 / 下)。
- 将文件保存在文件夹下,例如
MyApp并将其指定为baseHrefinindex.html。
相关link
【问题讨论】:
-
好像服务器认为JS文件是HTML文件...
-
我遇到了这个 b/c,由于某些缓存问题,我的浏览器试图获取一个不存在的 js 文件。一个简单的 CTRL+SHIFT+R 修复了它。
-
你找到答案了吗?从 Angular 7 升级到 Angular 8 后,我看到了同样的问题。
-
@Andrew 我面临的问题已通过接受的答案得到解决,我已将其摘要添加到问题本身中。
-
我赞成“Stack Underflow”,因为他/她关于“不存在的 js 文件”的评论为我指明了正确的方向。我遇到了由错误命名的 js 文件引起的类似问题。对我来说主要的收获是,如果找不到文件,你会得到这个令人困惑的 mime 类型错误。
标签: angular spring spring-boot angular8 tomcat9