vue-element-admin 打包部署到SpringBoot
更改vue里面vue.config.js
运行build命令 npm run build:prod
生成dist文件夹
打开你的springboot项目
复制static文件夹到 src/main/resources/ 并将index.html移动到templates(使用template)
更改index.html文件中导入地址
在colltroller层写一个控制器返回index.html
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class SpaController {
@RequestMapping("/index")
public ModelAndView forward() {
ModelAndView model = new ModelAndView();
model.setViewName("index");
return model;
}
}
配置WebMvcConfig
@Configuration
@ComponentScan({"com.Manage.config", "com.Manage.controller", "com.Manage.filter"})
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加拦截器,并指定需要拦截的路径
registry.addInterceptor(jwtInterceptor)//jwtInterceptor为自定义token拦截器
.addPathPatterns("/**") // 拦截所有路径,可以根据需要修改这个配置
.excludePathPatterns("/index","/static/**"); // 排除不需要拦截的路径
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// 将根路径 "/" 的请求重定向到 "/index.html"
registry.addViewController("/login").setViewName("forward:/index.html");
WebMvcConfigurer.super.addViewControllers(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
访问浏览器"http://localhost:你的端口号/index"即可