u12*_*123 5 java spring-boot vue.js
某种程度上基于本指南:
我创建了一个多模块 Maven 项目,其中一个子模块是我的后端,另一个子模块是我的前端。当我首先构建整个项目时,前端是“构建”,然后它的dist/资源被复制到后端,然后构建,我可以成功启动我的 Spring Boot 后端并java -jar target/backend-1.0.0-SNAPSHOT访问它localhost:8080
根据我在后端实现的控制器,这是有意义的:
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
@RequestMapping("/")
public Greeting root(@RequestParam(value = "name", defaultValue = "Root!") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
Run Code Online (Sandbox Code Playgroud)
如果我改为访问:http://localhost:8080/index.html我最终会进入我的前端:
目前有以下两条路线:
路由器.js
Vue.use(Router);
const router = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: HomeRoute
},
{
path: '/myroute',
name: 'myroute',
component: MyRoute
}
]
});
export default router;
Run Code Online (Sandbox Code Playgroud)
并在例如App.vue中我有:
<template>
<div class="hello">
<li>
<router-link to="/MyRoute">GoToMyRoute</router-link>
</li>
<li>
<router-link to="/">GoToHome</router-link>
</li>
<router-view></router-view>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
我也可以访问,例如:
到目前为止,一切都很好。但如果我尝试输入:http://localhost:8080/MyRoute但如果我尝试直接在浏览器中
@RequestMapping我认为这是因为我的控制器中缺少后端/MyRoute。
基于上述我的问题变成:
我建议你这样做:
在你的后端有一个“ui”控制器,它会将任何未映射的路由转发到你的前端应用程序,例如:
@RequestMapping(value = "/**/{[path:[^\\.]*}")
public String redirect() {
// Forward to home page so that route is preserved.
return "forward:/";
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
3869 次 |
| 最近记录: |