vue中router常见的三种传参方式
目录:我们在使用vue开发的过程中使用router跳转的时候肯定会遇到传参的情况;一般情况就三种传参是最常见的;那我们就来看看都有那几种传参方式吧!
第一种:
{
path: '/mall:id',
name: 'Mall',
component: lazyLoadMall('mall')
}
this.$router.push(`/mall${0}`)
获取参数:
console.log(this.$route.params) // 这儿原来写错了;感谢 @用户5158362048184 提出并修改
第二种:
this.$router.push({ name: 'Mall', query: { id: 0 } })
获取参数:
console.log(this.$route.query)
第三种:
this.$router.push({ name: 'Mall', params: { id: 0 } })
获取参数:
console.log(this.$route.params)
注意:第三种传参在刷新后数据会丢失;可以用JSON.stringify()进行传参避免在刷新以后数据丢失的情况
如果有什么不对的地方;欢迎大家补充;相互学习、共勉