本地环境vue与springboot联调

方案1: 前后端分离

  1. 启动Springboot服务
  2. 配置vue代理(前端项目根目录下的vue.config.js),转发API请求到后端服务
javascript 复制代码
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:2025', // Spring Boot 服务地址
        changeOrigin: true, // 支持跨域
        // pathRewrite: {
        //   '^/api': '', // 根据实际需求调整路径重写规则
        // },
      },
    },
  },
})
  1. 启动vue服务

方案2:前后端不分离

  1. 为Spring配置静态资源路径,指向vue项目的编译结果文件夹
bash 复制代码
-Dspring.resources.static-locations=classpath:/static/,file:/path/to/static/
  1. 如果使用的是较新的 Spring Boot 版本(如 2.4+)
bash 复制代码
spring的默认路径为:
            "classpath:/META-INF/resources/",
            "classpath:/resources/",
            "classpath:/static/",
            "classpath:/public/"
-Dspring.web.resources.static-locations=file:/absolute/path/to/static/dist/
  1. Spring中配置所有非API请求转发到vue的index页面
java 复制代码
@Controller
public class IndexController {

    /**
     * desc @RequestMapping("/{path:[^\\.]*}"):将非 API 的请求路径转发到 index.html。
     * 前提是你的 index.html 文件在 src/main/resources/static 或其他静态资源目录下。
     * @return :
     */
    @RequestMapping("/{path:[^\\.]*}")
    public String forwardToIndex() {
        return "forward:/index.html";
    }
}
  1. 启动Springboot项目
相关推荐
阳光是sunny2 小时前
走进微前端(1)手写single-spa核心原理
前端·javascript·vue.js
飞翔的佩奇2 小时前
基于SpringBoot+MyBatis+MySQL+VUE实现的名城小区物业管理系统(附源码+数据库+毕业论文+开题报告+部署教程+配套软件)
数据库·vue.js·spring boot·mysql·毕业设计·mybatis·小区物业管理系统
chancygcx_3 小时前
前端框架Vue3(二)——Vue3核心语法之OptionsAPI与CompositionAPI与setup
vue.js·前端框架
Pitayafruit3 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
spring boot·后端·ai编程
君莫笑几人回3 小时前
关于记录一下“bug”,在做图片上传的时候出现的小问题
java·开发语言·spring boot
烛阴3 小时前
Ceil -- 从平滑到阶梯
前端·webgl
90后的晨仔3 小时前
🔍Vue 模板引用(Template Refs)全解析:当你必须操作 DOM 时
前端·vue.js
90后的晨仔3 小时前
👂 Vue 侦听器(watch)详解:监听数据的变化
前端·vue.js
90后的晨仔4 小时前
深入浅出 Vue 的 computed:不仅仅是“计算属性”那么简单!
前端·vue.js