Vue 页面跳转携带参数方法详解
在 Vue 中,页面跳转可以使用路由进行实现,可以通过路由传递参数来携带参数。下面是具体的实现步骤:
- 在路由配置文件中,定义路由参数。例如:
{
path: '/user/:id',
name: 'user',
component: User
}
在这个例子中,路由参数为:id。
- 在页面中使用$router.push()方法进行跳转,并传递参数。例如:
this.$router.push({ name: 'user', params: { id: userId }})
在这个例子中,通过name指定要跳转的页面,通过params传递参数。
- 在目标页面中通过$route.params获取参数。例如:
export default {
name: 'User',
mounted() {
console.log(this.$route.params.id)
}
}
在这个例子中,通过this.$route.params获取路由参数。
这样就可以在 Vue 中实现页面跳转并携带参数了。
原文地址: https://www.cveoy.top/t/topic/luC6 著作权归作者所有。请勿转载和采集!