springboot 2.x 集成vue history模式的项目

由于springboot2.x中弃用了EmbeddedServletContainerCustomizer,在springboot2.x中使用的是WebServerFactoryCustomizer,
对于vue项目,有两种路由模式:hash,history,查看路由中的mode属性,若不是history,请忽略本博客。

hash模式中,访问地址栏会有#的出现,而history没有#,会更美观一些。在history模式中,项目打包后不能直接打开index.html,需要使用nginx或者集成在后端代码中,nginx请参考另一篇博客,当集成在springboot中,会有刷新或访问网址出现404报错,以下代码是解决方法。

vue中路由设置

const router = new VueRouter({
    mode: 'history',
    routes
})

springboot中 *****Application.java代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

@SpringBootApplication
public class MusicApplication {

    public static void main(String[] args) {
        SpringApplication.run(MusicApplication.class, args);
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> myWebServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
                factory.addErrorPages(error404Page);
            }
        };
    }

}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue2.x和Vue3.x是Vue.js的两个主要版本。Vue2.x是目前国内市场上最流行的前端应用开发框架之一,而Vue3.x则是最新版本。Vue2.x的官方文档可以在https://v2.cn.vuejs.org找到,而Vue3.x的官方文档可以在https://cn.vuejs.org找到。\[1\] 使用Vue2.x的步骤如下: 1. 下载Vue.js和Vuex模块。 2. 创建一个store文件夹,并在其中创建store.js文件,引入VueVuex。 3. 使用Vue.use引入Vuex。 4. 在main.js中引入store,并在实例化Vue对象时加入store对象。 5. 在组件中可以通过this.$store来使用store实例。\[2\] 在Vue2.x中,可以通过注册全局的方式来使用Vuex。在main.js中引入store,并在实例化Vue对象时加入store对象,然后就可以在组件中通过this.$store来使用store实例。\[2\] 在Vue3.x中,可以使用单库模式进行初始化。首先安装Vuex模块,然后在store/index.js中引入VueVuex,并使用Vue.use引入Vuex。在入口文件中引入store,并在实例化Vue对象时加入store对象。然后就可以在组件中通过this.$store来使用store实例。\[3\] #### 引用[.reference_title] - *1* [Vue2.x基础](https://blog.csdn.net/weixin_52218117/article/details/127270468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Vue系列之---vue2.0 Vuex](https://blog.csdn.net/duanhy_love/article/details/103148874)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值