在Vue Router中,如果设置“history”模式,则在Vue的包部署到Nginx或Tomcat下的时候,刷新画面,会出404错误。
mode:'history'
解决办法
Nginx
在配置文件中,加入如下内容,
try_files $uri $uri/ /index.html last;
Tomcat
在Vue工程的public目录下,新建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>my-app</display-name>
<description>
Tomcat Documentation.
</description>
<error-page>
<error-code>404</error-code>
<location>/index.html</location>
</error-page>
</web-app>
如果在index.html中,引入其他的js,则还会出现404的问题,还没有找到解决办法,有知道的,请留言,万分感谢。
简单粗暴的解决办法
将vue router模式中的
mode:'history'
注释掉。
网友评论