vue router 刷新404问题

5次阅读

共计 485 个字符,预计需要花费 2 分钟才能阅读完成。

1. 问题描述

单页面应用的 vue 项目,设置 router 模式为 history。可以跳转但刷新了页面,则显示为 404。

vue-router 的默认 hash 模式使用 URL 的 hash 来模拟一个完整的 URL,当 URL 改变时,页面不会重新加载。但是这种 hash 很丑,也不符合对 URL 的使用习惯。所以,需要使用另外一个叫 history 模式来实现 URL 跳转而无须重新加载页面。
export default new Router({
mode: “history”,
routes: [# other code]
)}

2. Apache 服务器配置
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
3. nginx 服务器配置
location / {
try_files $uri $uri/ /index.html;
}

正文完
 0