# 路由使用

## 1.路由跳转

```
this.$router.push('/home')
// 或者
this.$router.push({ path: '/home' })

// replace方法，不会向history添加记录
this.$router.replace('/home')
```

## 2.命名路由

```
this.$router.push({ name: 'home' })
```

## 3.动态路由匹配

```
// home.vue
this.$router.push('/detail/1')
// 或者
this.$router.push({ path: '/detail/1' })
// 或者
this.$router.push({ name: 'detail', params: { id: 1 } })

// detail.vue
// 获取id
let id = this.$route.patams.id
```

## 4.路由传参

```
// detail.vue
this.$router.push({ path: '/home', query: { name: 'Tom' } })

// home.vue
// 获取参数
let name = this.$route.query.name
```

## 5.路由回退

```
this.$router.back()
// 或者
this.$router.go(-1)
```

## 6.路由滚动

```
// router/index.js
scrollBehavior (to, from, savedPosition) {
  return { x: 0, y: 0 }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yicoding.gitbook.io/vue/lu-you/lu-you-shi-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
