我使用 Angular 6(前端)和 Kotlin/Java(后端)创建了一个 Spring 项目。我关注了JavaSampleApproach's tutorial (除了,我使用 Gradle 而不是 Maven)。
在 Angular 上,我关注了 Angular's routing guide .这是我设置路线的方式:
const routes: Routes = [
{path: 'clients', component: ClientsComponent},
{path: 'scopes', component: ScopesComponent},
{path: 'identity-providers', component: IdentityProvidersComponent},
{path: 'diagnostics', component: DiagnosticsComponent},
{path: '', redirectTo: '/clients', pathMatch: 'full'}
];
从那里开始,我首先在端口 8080 上运行 ng serve
,它运行良好。
然后,我运行 ng build --prod
,将它构建到 Spring 项目文件夹中。
在我的 src/main/kotlin/com.example.myproject
目录中,我创建了一个基于 @AndroidLover 's response 的 ViewController 类:
package com.example.platformadmintool
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class ViewController {
@RequestMapping("/clients", "/scopes", "/identity-providers", "/diagnostics")
fun routing() : String {
return "forward:/index.html"
}
}
(我明白@AndroidLover 的回应是针对 Angular 2 的。)
当我运行 gradle 命令 clean build bootRun
时,当我键入“http://localhost:8080”时会加载首页。通过单击导航,我能够清楚地遍历到“http://localhost:8080/diagnostics”和其他页面。
但是,如果我显式输入路由,如“http://localhost:8080/clients”,所有显示的页面都是 forward:/index.html
文本形式。
显然,错误出在我的请求映射中。我猜这与我返回一个字符串有关,但大多数在线解决方案似乎都使用 forward:/index.html
。
我不太熟悉 Spring 以及将它与 Angular 6 集成。如何配置我的路由以在 Spring 中工作?
最佳答案
在我看来,您对 *.js 或 *.css 等静态文件有疑问。尝试在浏览器中打开开发工具(在 Chrome 中为 F12)并检查当您询问静态文件时哪些文件发送给您的 bakend。您的应用程序很可能总是返回 index.html 而不是 *.js 或 *.css。 如果您的 GET 请求 URL 包含“.”,您必须将 Java 应用程序配置为返回静态文件。例如(java代码):
@RequestMapping(value = "/**/{[path:[^\\.]*}", method = RequestMethod.GET)
public String redirect() {
return "forward:/";
}
关于spring - Angular 6(前端)+ Spring : Configuring Routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332804/