vue打包整合到spring boot一记

背景

前段时间,根据需求,要将项目烧入到芯片,但我的擅长之处就是前后端分离开发,因此需要前端vue开发好,打包放到后端里面一起执行。那时候碰到好多貌似不可逾越的坑,今天终于可以抽出点时间,记录一下过程笔记。

vue-cli打包之前的配置

脚手架创建会生成如下的目录:
在这里插入图片描述

  1. 首先我们先把vue路由里的#去掉

只需在router里面加如下代码:

const router = new VueRouter({
  mode: "history",//去#
  ......
})
  1. 配置build文件夹下的index.js
build: {
    // Template for index.html
    index: path.resolve(__dirname, '../../static/public/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../../static/public'),
    assetsSubDirectory: 'static',
    assetsPublicPath: '/后端的context-path值/public/',

    /**
     * Source Maps
     */

    productionSourceMap: false,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: 'source-map',

    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
  1. 执行npm run build打包
    生成的包在项目根目录里的static文件夹下,直接把它拷贝到spring boot后台如下图即可
    在这里插入图片描述
  2. 启动后台并浏览器访问
    启动后台后,在浏览器里输入http://ip:端口号/项目名/static/public/index.html/index,如此大功告成。

总结

主要时build模块那里配置跟boot的指定的文件一致的,就可以访问到,有时候你会发现访问不到html,那是因为被boot拦截了。还有就是vue接口跨域的问题,最后交由后端来处理,尽量的使用prodEnv里面的变量。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值