Vue项目webpack打包部署时Tomcat刷新报404错误问题如何处理

Vue项目webpack打包部署时Tomcat刷新报404错误问题如何处理

Vue项目webpack打包部署时Tomcat刷新报404错误问题如何处理,Vue项目webpack打包部署时Tomcat刷新报404错误问题处理的注意事项有哪些,下面就是实战案例,一块儿来看一下html

遇到的问题

使用webpack打包vue后,将打包好的文件,发布到Tomcat上,访问成功,可是刷新后页面报404错。vue

在网上查找了一下,原来是HTML5 History 模式引起的问题,具体为何,vue官方已经给出了解释,你能够看https://router.vuejs.org/zh-cn/essentials/history-mode.htmljava

可是看完问题又来了,官方给出的解决方案中没有说tomcat下,怎么决解。webpack

解决方案

根据官方给出的解决方案原理web

你要在服务端增长一个覆盖全部状况的候选资源:若是 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面,这个页面就是你 app 依赖的页面。tomcat

因此在tomcat服务器下你能够这么作。在打包好的项目根目录下新建一个WEB-INF文件夹,在WEB-INF中写一个web.xml。 web.xml中写:服务器

<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就返回到 index.html 页面。app

最后还须要配置一下你的route,配置一个覆盖全部的路由状况,而后在给出一个 404 页面。ui

const router = new VueRouter({
 mode: 'history',
 routes: [
  {
    path: '*',
    component: (resolve) => require(['./views/error404.vue'], resolve)
  }
 ]
})
相关文章
相关标签/搜索