You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue3中vue-router如何去除URL的/#/前缀并保留自定义路径大小写

问题原因

你当前访问的URL自带#是因为使用了vue-router的哈希历史模式createWebHashHistory,该模式下所有路由路径都会作为哈希值拼接在域名后,不会触发浏览器页面重载,但无法和你配置的路径完全一致。

解决方案

Vue3版本下直接将哈希模式替换为HTML5历史模式即可,修改router/index.js文件如下:

  • 导入的API从createWebHashHistory替换为createWebHistory
  • 修改createRouter配置中的history参数对应值

修改后的完整代码:

import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () =>
      import('../views/Home.vue'),
    meta: { title: 'Home', description: 'Foo bar.' }
  },
  {
    path: '/About',
    name: 'About',
    component: () =>
      import('../views/About.vue'),
    meta: { title: 'About', description: 'Lorem ipsum.' }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

注意事项

项目部署到生产环境时,需要在Web服务端配置路由兜底规则,避免用户直接访问子路由时出现404错误。以Nginx为例,添加try_files $uri $uri/ /index.html;规则即可正常使用。

内容的提问来源于stack exchange,提问作者larkx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 16:45:04