$router.push方法怎么用
时间: 2023-11-21 20:47:31 浏览: 56
$router.push方法是Vue Router提供的方法,用于在程序中进行路由跳转。具体用法如下:
1. 在Vue组件中引入Vue Router:
```
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
```
2. 创建路由实例:
```
const router = new VueRouter({
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
}
]
})
```
3. 在Vue组件中使用$router.push方法进行路由跳转:
```
<template>
<div>
<button @click="goHome">Go Home</button>
<button @click="goAbout">Go About</button>
</div>
</template>
<script>
export default {
methods: {
goHome() {
this.$router.push({ path: '/' })
},
goAbout() {
this.$router.push({ name: 'About' })
}
}
}
</script>
```
在上面的例子中,我们创建了两个按钮,分别用于跳转到Home和About页面。在点击按钮时,我们调用了$router.push方法,传入一个包含路由信息的对象,来进行路由跳转。如果传入的是path属性,则会跳转到对应的路径;如果传入的是name属性,则会跳转到对应的路由名称。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)