vue 路由传参的三种方式

vue路由传参方式

params

// 用法:
{
   path:"/home/:name",
   name:"home",
   component:home
}
// 取值:
this.$router.params.name
//  缺点:刷新参数丢失
//  解决方案:在router.js文件中路由处添加占位符 
//  例如:/home/:name

query

 用法:router-link 参数   :to={
   path:"/home",
   name:"home",
   query:{
     name:"参数"
   }
}
取值:this.route.query.name
缺点:字符串类型参数刷新不丢失 对象数组类型刷新丢参
解决方案:將数组或对象参数转换为字符串类型传递

vuex + sessionStore

將参数数据存储到store中传递 同时保存到sessionStore中
用法:
//在页面刷新时将vuex里的信息保存到localStorage里 window.addEventListener("beforeunload",()=>{ 
localStorage.setItem("messageStore",JSON.stringify(this.$store.state)) 
}) 
//在页面加载时读取localStorage里的状态信息
localStorage.getItem("messageStore") && this.$store.replaceState(Object.assign(this.$store.state,JSON.parse(localStorage.getItem("messageStore"))));

你可能感兴趣的:(vue 路由传参的三种方式)