VUE打包去掉#

第一种方式tomcat配置

1. vue中mode:“history“的作用

在未设置mode:“history”,vue的路由默认是hash模式,地址栏中显示如下:
localhost:8080/#/…

  1. hash:在地址栏中显示"#"符号(这里的hash不是密码学中的散列运算)。例如:localhost:8080/#/index,hash的值为#/index。它的特点在于:hash虽然出现在路径中,但是不会被包括在HTTP请求中,对后端完全没有影响,因此改变hash不会重新加载页面。
  2. history利用了H5 history Interface中新增的pushState()和replaceState()方法。(需要特定浏览器支持)这两个方法应用于浏览器的历史记录栈,在当前已有的back、forward、go的基础上,它们提供了对历史记录进行修改的功能。只是当它们执行修改时,虽然改变了当前的Url,但是浏览器不会立即向后端发送请求。
  3. 综上:hash模式和history模式都属于浏览器自身的特性,Vue-Router只是利用了这两个特性(通过调用浏览器提供的接口)来实现前端路由。
    为了使路径更加直观及美观,就需要使用history模式。只需在router文件夹下的index.js中加入 mode:“history”。
let route = new Router({
    
    
    routes,
    mode: 'history'
})

2.后端tomcat配置转发页面

在conf/web.xml的底部配置vue路由错误转发页面。

 <error-page>
	<error-code>404</error-code>
	<location>/index.html</location>
  </error-page>

第二种方式nginx配置

const router = new Router({
    
    
  mode: 'history',  // 去掉路由地址的#
  routes: [
    {
    
    
      path: '/',  // 默认进入路由[写在第一个]
      redirect: '/home'   //重定向
    },
    {
    
    
      path: '/login',
      name: 'login',
      component: LoginPage
    },
    {
    
    
      path: '**',   // 错误路由[写在最后一个]
      redirect: '/home'   //重定向
    }
  ]
})

或

Nginx配置改为:

location / {
    
    
  try_files $uri $uri/ /index.html;
}

猜你喜欢

转载自blog.csdn.net/SmileSunshines/article/details/130483134