vue路由传参的两种方式,实现返回上个页面不刷新

我的项目是当在新增页面(下面叫A页面)先提交一些数据,然后跳转到下一个页面(下面叫B页面)再填写数据,然后返回到新增的页面
在这里插入图片描述
之前我直接跳转回B页面goback(),这样的话跳转回来A页面就什么数据都没有了

解决方法有两种,一种是在地址栏里面拿参数

 this.$router.push({
      name:'xxxxxxxxx',   // 跳转的页面名称
      params:{
        id:this.id || "xxxxxxxxxx"
        }
       })

在这里插入图片描述
在B页面拿取

 this.$route.params.id

然后在B页面再把ID传回之前A的页面

 this.$router.replace({
        name: "supervision-member-editor",
        params: {
          id: this.groupId
        }
      })

第二种是$roure官方文档提供的方法
https://router.vuejs.org/zh/guide/essentials/passing-props.html#%E5%B8%83%E5%B0%94%E6%A8%A1%E5%BC%8F
1,在A页面传参

      this.$router.push({
        name: "supervision-member-createPerson",
        params: {
          groupId:  this.groupId || this.id     //groupId类似于row.id
        }
      })
    },

2,在路由文件里面接收传值
在这里插入图片描述
ps:要传的ID啥的一定要在地址栏定义数据并且带过去
在这里插入图片描述

3,在B页面接收传值

 props: {
    id: { type: String, default: "" },
    groupId: {
      type: String,
      default: ""
    }
  },

4,在B页面传值回去

goBack() {
      this.$router.replace({
        name: "supervision-member-editor",
        params: {
          id: this.groupId
        }
      })
    },

最后还有一个在 localStorage存入你想要传的值,然后再取,但是这方法不好


 localStorage.setItem('lid', 123456)
 
var getId = localStorage.getItem('id');

最后还可以使用vuex存储

-----------------------------------分界线-----------------------------------------
组件内直接使用的

        <el-table-column
          label="字典类型"
          align="left"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <router-link
              :to="'/system/dict-data/index/' + scope.row.dictId"
              class="link-type"
            >
              <span>{{ scope.row.dictType }}</span>
            </router-link>
          </template>
        </el-table-column>
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue路由传参两种方式。第一种是使用params传参。在路由配置中,可以在path中添加对应的参数,例如`/:param`。在跳转页面时,可以使用`this.$router.push({name: 'test', params: {name: 'tom'}})`来传递参数。在接收参数页面中,可以使用`this.$route.params.name`来获取传递的参数。这种方式的缺点是,当页面刷新后,传递的参数会变成undefined。\[2\] 第二种方式是使用query传参。在跳转页面时,可以使用`this.$router.push({path: '/test', query: {name: 'tom'}})`来传递参数。在接收参数页面中,可以使用`this.$route.query.name`来获取传递的参数。这种方式的优点是,即使页面刷新,传递的参数也不会丢失。\[3\] #### 引用[.reference_title] - *1* [Vue路由传参两种方式](https://blog.csdn.net/weixin_41080999/article/details/80379441)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [VUE路由传参两种方式](https://blog.csdn.net/weixin_42250577/article/details/115296970)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值