angular - Angular 项目不适用于 Spring Boot
问题描述
我想要做的是将前端应用程序与 Spring Boot 后端一起运行。
使用ng serve
.
我尝试使用ng build --prod
然后将文件从dist
文件夹复制到src/main/resources/static
. 之后我用来clean install
生成 jar 文件并使用java -jar %filename.jar%
.
Spring Boot 应用程序似乎正在正确启动,REST 端点可用,但是,当我尝试在 localhost:8080 下获取前端视图时,我在终端中获得了Whitelabel 错误页面和No mapping for GET /
信息。
假设是否正确,例如,如果 localhost:4200/products 显示带有某些产品的表(ng serve
方法),那么 localhost:8080/products(在 jar 方法中)应该显示同一个表?还是我错过了一些步骤来实现它?
我尝试使用哈希策略,但它似乎没有改变任何东西。
应用程序路由.module.ts:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'sign-up', component: SignUpComponent },
{ path: 'login', component: LoginComponent },
{ path: 'products', component: ProductsComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true })],
exports: [RouterModule]
})
export class AppRoutingModule { }
索引.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">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.cd71f88743b54f1de66b.css"></head>
<body>
<app-root></app-root>
<script src="runtime.acf0dec4155e77772545.js" defer></script><script src="polyfills.3ea10c1644fbb8005130.js" defer></script><script src="scripts.a73844de5f291be94c3b.js" defer></script><script src="main.abbc3215607e4963f8f1.js" defer></script></body>
</html>
解决方案
“/”是您要调用的默认 GET URL 吗?我想您可以通过直接在浏览器中粘贴他们的 GET URL 来访问其他端点吗?如果是这种情况,您能否检查浏览器网络日志中调用的 URL 是什么?如果这一切看起来都很好,那可能是 Angular 路由的问题。如果您使用的是 HashLocationStrategy,请尝试在 index.html 文件的头部添加。
推荐阅读
- c# - 从 HoloLens 2 访问计算机上的数据库文件
- android - 从外部下载文件夹加载文件以在 Flutter 应用程序中使用
- google-chrome - Chrome 会话超时并中断页面。仅在 Chrome 上出现问题并在其他浏览器中工作
- azure - 在 Appinsigts 中,确定查询返回的时间戳是否 > 24 小时前
- python - Drive API - 使用 Python 保存文件 ID
- windows-subsystem-for-linux - 如何使用多个命令启动 Windows 终端
- scala - Spark 数据框未使用工作人员
- python - BeautifulSoup 没有找到所有 tr 标签
- android - 无法将整个 arrayList 添加到 ChipGroup
- c# - c#如何将所有字典放入Dictionary的派生类型对象中?