簡體   English   中英

Angular 項目不適用於 Spring 引導

[英]Angular project doesn't work with Spring Boot

我要做的是與 Spring 引導后端一起運行前端應用程序。

使用ng serve后,Angular 應用程序在 localhost:4200 下正常工作。

我嘗試使用ng build --prod然后將文件從dist文件夾復制到src/main/resources/static 之后,我使用全新clean install生成 jar 文件並使用java -jar %filename.jar%執行它。

Spring 引導應用程序似乎正在正確啟動,REST 端點可用,但是,當我嘗試在 localhost:8080 下獲取前端視圖時,我在終端中收到Whitelabel 錯誤頁面No mapping for GET /

假設是否正確,例如,如果 localhost:4200/products 顯示帶有某些產品的表( ng serve方法),那么 localhost:8080/products (在 jar 方法中)應該顯示同一個表? 還是我錯過了一些步驟來實現它?

我嘗試使用 hash 策略,但似乎沒有任何改變。

應用程序路由.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 來將 go 連接到 rest 端點? 如果是這種情況,您能檢查一下您在瀏覽器網絡日志中看到的調用的 URL 嗎? 如果這一切看起來都很好,那可能是 Angular 路由的問題。 如果您使用的是 HashLocationStrategy,請嘗試在 index.html 文件的頭部添加。

最終對我有用的是從配置類之一中刪除@EnableWebMvc注釋。 在這里找到答案: https://stackoverflow.com/a/33852040/14908618

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM