Vue部署到tomcat
前置条件:history模式
根路径:/xxx
解决:
1.修改打包配置文件,通常在config/index.js,将
assetsPublicPath: '/'
改为assetsPublicPath: '/xxx/'
2.进入tomcat/webapps目录创建文件夹xxx,将打包好的dist文件夹中的所有文件复制到xxx。此时访问即可正常访问,但是页面刷新时会报404,继续下面操作
3.在该文件夹下创建WEB-INF文件夹,创建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>
这样,刷新页面就不会404了.
tip --> 如果根路径是/aaa/bbb,tomcat也需要创建目录/aaa/bbb,将WEB-INF放置到aaa目录下,并将web.xml中的
/index.html
改为/bbb/index.html