Vue Router 提供了兩種主要的路由模式:Hash 模式和 History 模式。每種模式都有其特定的使用場(chǎng)景和配置要求。
http://localhost:3002/#/dashboard
#
符號http://localhost:3002/dashboard
#
符號編輯 src/router/index.ts
文件:
import { createRouter, createWebHashHistory } from 'vue-router';
import { routes } from './routes';
const router = createRouter({
// 使用 Hash 模式
history: createWebHashHistory(),
routes,
scrollBehavior() {
return {
el: '#app',
top: 0,
behavior: 'smooth',
};
},
});
export default router;
import { createRouter, createWebHistory } from 'vue-router';
import { routes } from './routes';
const router = createRouter({
// 使用 History 模式
history: createWebHistory(import.meta.env.VITE_BASE_URL || '/'),
routes,
scrollBehavior() {
return {
el: '#app',
top: 0,
behavior: 'smooth',
};
},
});
export default router;
在 .env
文件中配置基礎路徑:
# 基礎路徑配置(History 模式需要)
VITE_BASE_URL=/
# 如果部署在子目錄,例如:
# VITE_BASE_URL=/my-app/
編輯 vite.config.ts
文件:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [vue()],
base: process.env.VITE_BASE_URL || '/',
server: {
port: 3002,
host: '0.0.0.0',
// History 模式開(kāi)發(fā)服務(wù)器配置
historyApiFallback: {
index: '/index.html',
},
},
});
server {
listen 80;
server_name your-domain.com;
root /path/to/your/dist;
index index.html;
# History 模式關(guān)鍵配置
location / {
try_files $uri $uri/ /index.html;
}
# 靜態(tài)資源緩存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}
在項目根目錄創(chuàng )建 .htaccess
文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
const express = require('express');
const path = require('path');
const app = express();
// 靜態(tài)文件服務(wù)
app.use(express.static(path.join(__dirname, 'dist')));
// History 模式支持
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
app.listen(3000);
# .github/workflows/deploy.yml
name: Deploy to GitHub Pages
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
創(chuàng )建 vercel.json
文件:
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/index.html"
}
]
}
創(chuàng )建 _redirects
文件:
/* /index.html 200
原因: 服務(wù)器沒(méi)有正確配置 fallback
解決方案:
try_files
或類(lèi)似的 fallback 機制base
路徑配置是否正確配置示例:
// 部署在 /my-app/ 子目錄
const router = createRouter({
history: createWebHistory('/my-app/'),
routes,
});
# .env
VITE_BASE_URL=/my-app/
解決方案:
const router = createRouter({
history: createWebHistory(
import.meta.env.MODE === 'development'
? '/'
: import.meta.env.VITE_BASE_URL
),
routes,
});
createWebHistory
或 createWebHashHistory
VITE_BASE_URL
vite.config.ts
的 base
選項選擇合適的路由模式對項目的用戶(hù)體驗和部署便利性都有重要影響,建議根據具體需求進(jìn)行選擇。