vue本地使用history除了根路径外其他路径都无法显示
时间: 2023-07-16 07:15:12 浏览: 45
这个问题可能是由于 Vue Router 配置不正确导致的。请确认以下几点:
1. 确保在 `router/index.js` 文件中正确配置了 Vue Router。
例如:
```javascript
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
```
2. 确保在服务器端设置了正确的路由重定向,以便将所有的请求都指向 `index.html`。
如果你使用的是 Apache 服务器,可以在 `.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>
```
如果你使用的是 Nginx 服务器,可以在配置文件中添加以下代码:
```
location / {
try_files $uri $uri/ /index.html;
}
```
完成上述配置后,应该可以在本地使用 Vue Router 的 history 模式了。