$router.history.current方法
时间: 2024-06-07 09:08:21 浏览: 8
$router.history.current是Vue Router提供的一个属性,它可以返回当前活跃的路由对象,包含了当前路由的各种信息,如路径、参数、查询等。具体而言,$router.history.current包含以下属性:
- path:当前路由路径
- hash:当前路由的哈希值
- query:当前路由的查询参数
- params:当前路由的路径参数
- fullPath:当前路由的完整路径(包括查询参数和哈希值)
- matched:一个数组,包含当前路由的所有嵌套路径的路由记录对象
通过访问$router.history.current可以获取当前路由的各种信息,可以用于实现路由导航、动态路由匹配等功能。
相关问题
{{$route.meta.title}}
根据引用内容,可以得知$route实际上是根组件$root的_route属性。它并没有定义在data里,而是通过调用Vue.util.defineReactive(this, '_route', this._router.history.current)进行响应式监听。所以在组件实例化时,_route对象被赋值为xxx,才会触发响应式监听。而定义在data里的数据在组件实例化时会被初始化。所以{{$route.meta.title}}将会显示当前路由的meta中的title属性。
router.matcher
`router.matcher` is a method in Vue Router that returns a function used to match a given location against the routes defined in the router. It takes a single parameter, which is the location object to be matched.
The returned function has the following signature:
```javascript
function (location: Location, currentRoute?: Route, redirectedFrom?: Location): Route | undefined
```
The `location` parameter is the location object to be matched. The `currentRoute` parameter is an optional current route object, which can be used to help with nested routes. The `redirectedFrom` parameter is an optional location object that represents the original location before any redirects were applied.
The function returns a `Route` object if there is a match, or `undefined` if there is no match.
Here is an example of how to use `router.matcher`:
```javascript
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/contact', component: Contact }
]
})
const location = { path: '/about' }
const matchedRoute = router.matcher(location)
console.log(matchedRoute) // Route { path: '/about', component: About }
```
相关推荐
![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)