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

Vue中如何自定义vue.config.js配置页面的访问路由?

解决方案

有两种常用实现方案,推荐优先使用第一种,无需额外配置服务器规则:

方案1:修改pages配置的filename属性

直接调整otherpage页面对应的filename配置项,让打包后生成other/index.html结构,静态服务器会默认自动匹配目录下的index.html文件,直接通过domain/other即可访问:

module.exports = {
  pages: {
    index: {
      entry: 'src/main.js',
      template: 'public/index.html',
      filename: 'index.html'
    },
    otherpage: {
      entry: 'src/other.js',
      template: 'public/otherpage.html',
      // 仅修改这一行即可
      filename: 'other/index.html'
    }
  }
}

方案2:保留原有pages配置,额外配置路由重写

如果不想修改打包产物的目录结构,可以通过重写规则把/other路径映射到/otherpage.html:

开发环境配置

在vue.config.js中追加devServer配置:

module.exports = {
  // 原有pages配置保持不变
  pages: { /* 你的原有pages配置 */ },
  devServer: {
    historyApiFallback: {
      rewrites: [
        { from: /^\/other$/, to: '/otherpage.html' }
      ]
    }
  }
}

生产环境配置

根据你使用的服务器配置重定向规则,以Nginx为例,新增如下配置即可:

location /other {
  try_files $uri $uri/ /otherpage.html;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 13:45:00