# 把网站部署在一级路径
Vite + Vue 3 项目中,如果你想(例如 https://example.com/myapp/),你需要在两个地方设置:
# ✅ 1. 在 vite.config.js 中设置 base
Vite 会根据 base 生成正确的静态资源路径。
# 👉 示例:部署到 https://example.com/myapp/
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
base: '/myapp/' // 你的网站一级路径
})
如果你的项目直接放在根目录(https://example.com/),则:
base: '/'
# ✅ 2. Vue Router 需要设置 base(如使用 history 模式)
如果你使用 Vue Router 4 + history 模式:
# router/index.js
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory('/myapp/'), // 与 Vite 的 base 一致
routes: [...]
})
export default router
如果使用 hash 模式,则无需设置 base:
createWebHashHistory()
# 🟦 Nginx 示例配置(重要)
location /myapp/ {
try_files $uri $uri/ /myapp/index.html;
}