vue router hash 和 history
时间: 2023-10-28 09:55:14 浏览: 50
Vue Router 提供了两种路由模式:hash 模式和 history 模式。
1. Hash 模式:
在 hash 模式下,URL 中的路由会以 "#/" 开头,例如:http://example.com/#/home。这种模式下,路由的改变不会导致页面的刷新,而是通过监听 URL 的 hash 值变化来实现路由切换。Vue Router 默认使用的就是 hash 模式。
2. History 模式:
在 history 模式下,URL 中的路由不再带有 "#/",例如:http://example.com/home。这种模式下,路由的改变不会有 "#" 符号出现在 URL 中,看起来更加美观。但是需要注意的是,history 模式需要服务器端的支持,因为在 history 模式下刷新页面或直接访问某个 URL 时,会向服务器发送请求,所以需要配置服务器以返回正确的页面。
你可以通过以下方式配置 Vue Router 的路由模式:
```javascript
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
const router = createRouter({
history: createWebHashHistory(), // 使用 hash 模式
// history: createWebHistory(), // 使用 history 模式
routes: [
// 路由配置
],
});
```
其中,`createWebHashHistory()` 创建 hash 模式的路由实例,`createWebHistory()` 则创建 history 模式的路由实例。根据需要选择相应的模式进行配置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)