路由router.push传参实现数据接收

实现一个系统的新增编辑按钮,如果是通过ruoter.push实现跳转到其它组件的功能,那么在新增和编辑的时候会传递数据,到下一个页面会进行接收数据,在获取该数据渲染即可

1.效果图

新增:

编辑:

可见当点击新增按钮时传递的type值为add,点击编辑按钮时传递的type值为edit ,因此点击编辑会通过router.push传参接收数据并且渲染数据

2.实现代码

新增按钮组件页面

//注册
const goToRegister = () => {
  router.push({name:'register', params: { type: 'add' } })
}
//编辑
const toRegister = async(record) => {
  const Data = await getSourceApiDetail(record)
 
  const allData = {...Data.data.data,id:record.id}
  console.log(allData,'>>>>00000<<<<');
  const {
    id,
    apiName,
    apiDirectoryId,
    apiSource,
    apiAgrement,
    apiPort,
    apiPath,
    apiRequest,
    apiTimeout,
    apiDescription,
    apiInputInfoDTOList,
    apiRequestBodyDTOList,
    apiResponseInfoDTOList
  } = allData
  console.log(allData.apiInputInfoDTOList, '5550000000555')
  router.push({ 
    name: 'register' ,
    params: {
      type: 'edit',
      id,
      apiName,
      apiDirectoryId,
      apiSource,
      apiAgrement,
      apiPort,
      apiPath,
      apiRequest,
      apiTimeout,
      apiDescription,
      apiInputInfoDTOList: JSON.stringify(apiInputInfoDTOList),
      apiRequestBodyDTOList: JSON.stringify(apiRequestBodyDTOList),
      apiResponseInfoDTOList: JSON.stringify(apiResponseInfoDTOList),
    }
  })
}

跳转页面

const { type,id,
    apiName ='',
    apiDirectoryId =[],
    apiSource ='',
    apiAgrement ='',
    apiPort ='',
    apiPath ='',
    apiRequest ='',
    apiTimeout ='',
    apiDescription ='',
    apiInputInfoDTOList = '',
    apiRequestBodyDTOList = '',
    apiResponseInfoDTOList = ''
  } = route.params
    console.log(route.params,'>>....>>>');
// 表单验证
const formState = ref({
  apiName: '',
  apiDirectoryId: [],
  apiSource: '',
  apiAgrement: '',
  apiPort: '',
  apiPath: '',
  apiRequest: '',
  apiTimeout: '',
  apiDescription: ''
})
onMounted(() => {
  // 从路由状态中获取数据
  const type = route.params.type;
  
  if (type === 'goback') {
    formState.value = Object.assign({}, route.params);
    
  } else if(type === 'edit'){
    formState.value = Object.assign({}, route.params);

  }

});

注意:参数以及绑定的数据源等信息根据实际修改!!!

在我的代码中我点击新增编辑之后到下一个页面之后还有跳转到下一个页面,也是通过该方法传递数据实现接收,第二个页面我通过onMounted函数来接收,然后我再根据我传递的type类型来实现具体的新增修改逻辑

这是最后一个页面的接收数据,仅参考

onMounted(() => {
  if (type === 'add') {
    formData.value = route.params
    console.log(route.params,'......');
    
  } else if(type === 'edit'){
    dataSourceOne.value = JSON.parse(route.params.apiInputInfoDTOList)
  dataSourceNext.value = JSON.parse(route.params.apiRequestBodyDTOList)
  dataSourceThird.value = JSON.parse(route.params.apiResponseInfoDTOList)
  testDetails.value = JSON.parse(route.params.apiRequestBodyDTOList)
  testDetailsNext.value = JSON.parse(route.params.apiInputInfoDTOList)
  }
  
})

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值