|  | 
|  | 1 | +--- | 
|  | 2 | +title: 路由的 hash 模式 和 history 模式的区别是什么? | 
|  | 3 | +date: 2024-11-05T03:44:11Z | 
|  | 4 | +slug: post-24 | 
|  | 5 | +author: chaseFunny:https://github.com/chaseFunny | 
|  | 6 | +tags: ["AI"] | 
|  | 7 | +--- | 
|  | 8 | + | 
|  | 9 | +让我详细解释一下路由的 hash 模式和 history 模式的区别: | 
|  | 10 | + | 
|  | 11 | +1. URL 格式区别: | 
|  | 12 | +- Hash 模式:使用 # 号,例如:`www.example.com/#/home` [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 13 | +- History 模式:更像传统的 URL,例如:`www.example.com/home` [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 14 | + | 
|  | 15 | +2. 工作原理区别: | 
|  | 16 | +- Hash 模式: | 
|  | 17 | +  - 使用 URL 的 hash(#)部分来模拟完整的 URL | 
|  | 18 | +  - hash 部分不会被发送到服务器 | 
|  | 19 | +  - 基于浏览器的 hashchange 事件 [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 20 | + | 
|  | 21 | +- History 模式: | 
|  | 22 | +  - 基于 HTML5 的 History API | 
|  | 23 | +  - 使用 pushState 和 replaceState 方法来管理历史记录 | 
|  | 24 | +  - URL 更美观,更像传统的服务器端路由 [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 25 | + | 
|  | 26 | +3. 服务器配置要求: | 
|  | 27 | +- Hash 模式: | 
|  | 28 | +  - 不需要特殊的服务器配置 | 
|  | 29 | +  - 因为 hash 部分不会发送到服务器 | 
|  | 30 | +  - 对 SEO 不太友好 [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 31 | + | 
|  | 32 | +- History 模式: | 
|  | 33 | +  - 需要服务器配置支持 | 
|  | 34 | +  - 需要配置所有路由都返回 index.html | 
|  | 35 | +  - 否则会出现 404 错误 [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 36 | + | 
|  | 37 | +4. 服务器配置示例(History 模式): | 
|  | 38 | + | 
|  | 39 | +Apache 配置: | 
|  | 40 | +```apache | 
|  | 41 | +<IfModule mod_rewrite.c> | 
|  | 42 | +  RewriteEngine On | 
|  | 43 | +  RewriteBase / | 
|  | 44 | +  RewriteRule ^index\.html$ - [L] | 
|  | 45 | +  RewriteCond %{REQUEST_FILENAME} !-f | 
|  | 46 | +  RewriteCond %{REQUEST_FILENAME} !-d | 
|  | 47 | +  RewriteRule . /index.html [L] | 
|  | 48 | +</IfModule> | 
|  | 49 | +``` | 
|  | 50 | + | 
|  | 51 | +Nginx 配置: | 
|  | 52 | +```nginx | 
|  | 53 | +location / { | 
|  | 54 | +  try_files $uri $uri/ /index.html; | 
|  | 55 | +} | 
|  | 56 | +``` | 
|  | 57 | + | 
|  | 58 | +5. 如何选择: | 
|  | 59 | + | 
|  | 60 | +- 选择 Hash 模式如果: | 
|  | 61 | +  - 不想配置服务器 | 
|  | 62 | +  - 项目不需要考虑 URL 的美观性 | 
|  | 63 | +  - 对 SEO 要求不高 | 
|  | 64 | + | 
|  | 65 | +- 选择 History 模式如果: | 
|  | 66 | +  - 需要更好的 URL 外观 | 
|  | 67 | +  - 项目需要更好的 SEO | 
|  | 68 | +  - 可以控制服务器配置 | 
|  | 69 | + | 
|  | 70 | +6. 兼容性考虑: | 
|  | 71 | +- Hash 模式:兼容性更好,支持更多的浏览器 | 
|  | 72 | +- History 模式:需要 HTML5 History API 支持,较新的浏览器都支持 | 
|  | 73 | + | 
|  | 74 | +7. 迁移建议: | 
|  | 75 | +如果要从 Hash 模式迁移到 History 模式: | 
|  | 76 | +1. 确保服务器已正确配置 | 
|  | 77 | +2. 更新路由配置 | 
|  | 78 | +3. 测试所有路由是否正常工作 | 
|  | 79 | +4. 检查直接访问路由是否正常 [2](https://router.vuejs.org/guide/essentials/history-mode) | 
|  | 80 | + | 
|  | 81 | +在实际开发中,如果你使用的是现代前端框架(如 Next.js、Umi 等),它们通常会默认使用 History 模式并自动处理服务器配置,使开发者可以专注于业务逻辑的开发。 | 
|  | 82 | + | 
|  | 83 | +--- | 
|  | 84 | +来源:Claude 3.5 Sonnet  | 
|  | 85 | + | 
|  | 86 | +--- | 
|  | 87 | +此文自动发布于:<a href="https://github.com/coderPerseus/blog/issues/24" target="_blank">github issues</a> | 
0 commit comments