官方文档的相关解释:
Vue.js官方文档,文档中有关于Apache、nginx、原生 Node.js的解决方案,但是没有tomcat的解决方法。下面介绍一种我亲测有效的方法。
1.将vue项目打包好放在服务器项目中,
2.在文件index.html 目录位置新建一个 WEB-INF文件夹。
3.在其中新建一个web.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
<display-name>Router for Tomcat</display-name>
<error-page>
<error-code>404</error-code>
<location>/index.html</location>
</error-page>
</web-app>
4.还有一点,我们要在vue 的 router中配置一个类似这样的路由:
routes: [
{
path: '*',
component: error
}
]
覆盖所有的路由情况,然后在给出一个 404 页面