SpringBoot集成vue的开发解决方案

作者:sunct 时间:2023-11-24 20:58:10 

最近由于工作要求:前端采用vue开发,后端采用springboot开发,前后端分离开发,最后前端页面又整合到后端来。经历多次采坑,总结以下方案:

vue build后的文件部署到springboot目录

vue打包后,会生成dist目录

SpringBoot集成vue的开发解决方案

springboot静态资源目录如下:

SpringBoot集成vue的开发解决方案

SpringBoot处理静态资源和页面,设置如下:


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}

页面模板设置,如下:


#页面模板设置
spring.thymeleaf.option=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

部署方案
dist的index.html 直接放在templates目录下
dist的css、fonts、img、js 直接放在static目录下

vue打包后vendor文件过大的优化方案

vue使用vue-cli打包后,vendor就将vue.js其他引用的包一起压缩打包进去,导致vendor文件超过1M,影响页面加载速度
本项目使用了vue、vue-router、iview、axios、echarts等
(1)使用vue、vue-router、iview、axios、echarts等cnd引用
在index.html文件中,增加:


<script src="https://unpkg.com/vue@2.5.2/dist/vue.min.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.min.js"></script>
<script src="https://unpkg.com/iview@3.4.0/dist/iview.min.js"></script>
<script src="https://unpkg.com/axios@0.18.0/dist/axios.min.js"></script>
<script src="https://unpkg.com/echarts@4.2.1/dist/echarts.min.js"></script>

(2)打包时,排除vue、vue-router、iview、axios、echarts等打包
在webpack.base.conf.js文件中,module.exports={…} 方法中添加


module.exports = {
...
externals:{
 'vue':'Vue',
 'axios':'axios',
 'vue-router':'VueRouter',
 'iview':'iview',
 'echarts':'echarts',
 },
 ...
}

这里有注意的是:命名问题
比如:vue-router的在https://unpkg.com/vue-router@3.0.1/dist/vue-router.min.js中默认采用VueRouter,所以在import vue-router一定要使用VueRouter,而不能使用其他别名。

vue默认别名是Vue
axios默认别名是axios
vue-router默认别名是VueRouter
iview默认别名是iview
echarts默认别名是echarts

例子:


import Vue from 'vue'
import VueRouter from 'vue-router'
import iview from 'iview'
import echarts from 'echarts'

Vue.use(VueRouter)

export default new VueRouter({
mode: 'history',
...
})

(3)vue-router的路由页面设置为按需加载


export default new VueRouter({
mode: 'history',
routes: [
 //页面1
 {
  path: '/Page1',
  name: 'page1',
  component: () => import('@/views/Page1.vue')
 },
 //页面2
 {
  path: '/Page2',
  name: 'page2',
  component: () => import('@/views/Page2.vue')
 }
]
});

(4)重新打包后vendor.js文件就小了,可以小到1k哦

vue-router使用了history模式,vue其实做的是单页面应用,但是效果看上去是多个不同页面,问题来了,部署到线上环境后,该如何配置?

百度上有很多处理方案,比如:使用errorPage方式处理,nginx配置等,这些问题比较繁琐,而且在部署过程中,一堆问题。
经过多次尝试,发现有一个最简单方法,Controller直接url路径配置到index.html即可,而且轻松解决history模式带来的后端问题,如下:


@ApiOperation(value = "", hidden = true)
@RequestMapping(path = "/Page1")
public String page1() {
 return "index";
}

@ApiOperation(value = "", hidden = true)
@RequestMapping(path = "/Page2")
public String page2() {
 return "index";
}

这种方案非常有利于后端开发人员做更多的进一步操作,比如:给每个页面增加页面权限等。
注意:该方案目前适用于前端页面整合到后端的项目工程。

来源:https://blog.csdn.net/sunct/article/details/91047721

标签:SpringBoot,集成,vue
0
投稿

猜你喜欢

  • Android基础知识之frame动画效果

    2022-07-10 03:46:17
  • Android使用TextView跑马灯效果

    2022-05-20 20:07:20
  • C#调用VB进行简繁转换的方法

    2023-02-25 23:19:43
  • Android 静默安装实现方法

    2021-08-04 05:11:25
  • C#集合之字典的用法

    2022-04-11 23:27:19
  • SpringBoot实现WebSocket即时通讯的示例代码

    2022-06-14 19:59:36
  • Java面向对象程序设计:继承,多态用法实例分析

    2021-08-03 06:54:04
  • 使用SpringBoot配置多数据源的经验分享

    2022-05-25 04:02:57
  • android实现拍照或从相册选取图片

    2022-08-08 06:55:36
  • Java8新特性之默认方法(default)浅析

    2023-10-03 10:41:13
  • 解决Maven本地仓库明明有对应的jar包但还是报找不到的问题

    2022-07-21 23:12:07
  • java 单例模式和工厂模式实例详解

    2023-04-07 22:10:19
  • sharding-jdbc5.0.0实现分表实践

    2023-12-07 10:12:26
  • Android App自动更新之通知栏下载

    2023-11-07 16:56:45
  • .Net Winform开发笔记(四)透过现象看本质

    2022-06-22 09:36:24
  • C# Path类---文件路径解读

    2022-05-20 05:58:47
  • Android使用SmsManager实现短信发送功能

    2023-08-24 17:54:20
  • 轻松学习C#的装箱与拆箱

    2021-07-01 12:11:51
  • Spring Boot + Mybatis 实现动态数据源案例分析

    2023-05-15 12:13:47
  • spring-boot使用Admin监控应用的方法

    2023-06-28 19:58:39
  • asp之家 软件编程 m.aspxhome.com