【vue】vue路由传参两种方式区别-刷新pq参数不消失-np参数消失

本文探讨了在Vue中通过`this.$router.push`进行路由传参时,query参数(pq)在刷新后仍保留,而params参数(np)会消失。同时,提供了一种在main.js中封装路由跳转方法以确保刷新后参数不丢失的解决方案。
摘要由CSDN通过智能技术生成

this.$router.push({path:"/demo",query:{id:1}});

console.log(this.$route.query);刷新【路由展示参数】,刷新不丢失pq

this.$router.push({name:"demo",params:{id:1}});

console.log(this.$route.params);刷新【路由不展示】传递参数消失np;

对路由的封装,无论如何都展示在路由中,刷新参数不丢失

1.在main.js封装方法

1.1 $jump

//刷新页面参数都不会消失
Vue.prototype.$jump = ({ url, params = {}, name, type = 'push' } = {}) => {
  //使用name传递参数
  if (name) {
    currentVue.$router[type]({
      name,
      query: params
    })
  }
    //使用path传递参数
  if (url) {
    currentVue.$router[type]({
      path: url,
      query: params
    })
  }
}

1.2 currentVue

const currentVue = new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

1.3页面使用

click() {//路由展示 使用path传递参数--刷新页面不消失
      let url = "/test";
      this.$jump({
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端小云儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值