Vue3.2中通过路由向组件中传参

问题描述

通过访问/error/info,组件中显示具体的info值,如:/error/系统出错,在组建中就显示“系统出错”

解决方案

方法一:

 路由代码

   {
      path: '/fail/:info',
      name: 'fail',
      component: () => import('@/views/error/fail.vue'),
   }

组件代码

<script setup lang="ts">
import { useRoute } from 'vue-router'

const route = useRoute()
const info = route.params.info
</script>

<template>
    <el-empty :description="info" class="empty">
    </el-empty>
</template>

<style scoped>
.empty{
    height: 100vh;
    width: 100vw;
}
</style>

方法二

路由代码

    {
      path: '/fail/:info',
      name: 'fail',//授权失败页面
      component: () => import('@/views/error/fail.vue'),
      props: true // 将参数作为props传递给组件
    },

组件代码

<script setup lang="ts">
import { toRefs,defineProps} from 'vue'

const props = defineProps({
     info: String,
})
const {info} =toRefs(props)
</script>

<template>
    <el-empty :description="info" class="empty">
    </el-empty>
</template>

<style scoped>
.empty{
    height: 100vh;
    width: 100vw;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值