vue页面跳转 路径传参方式和meta传参

4 篇文章 0 订阅

 vue2

参数三个形式

 获取当前路由对象

 console.log(this.$route);

获取当前路由

 console.log(this.$route.path);

 1.query传参

 获取: this.$route.query.size(参数名)

2. params传参

 

 路由上面写接收参数的变量  /:id

获取: this.$route.params.id(参数名)

3.meta参数

在router页面的路由配置里面写

           {
                path: "index",
                name: "index",
                component: () => import('@/views/Index/Index.vue'),
                props: true,
                meta: { title: "首页" },
            },

获取:this.$route.meta.title(参数名)

修改: 只能在路由监听里面修改才生效

watch监听路由跳转

  watch: {
    $route: {
      immediate: true,
      handler(val, oldval) {
        // console.log(val, oldval);
        val.meta.keepAlive = true
        let arr: any = sessionStorage.getItem("crumbs")
        // console.log("获取面包屑列表", JSON.parse(arr));
        this.crumbs = JSON.parse(arr)
        // console.log("面包屑",this.crumbs);
      }
    },
  },

 路由跳转监听函数里面

router.beforeEach((to, from, next) => {
  to.meta.title = "新值"
});

vue3

{
    path: "/mtteacher/:id",
    name: "mtteacher",
    component: () => import("@/views/MT/teacher/index.vue"),
    props: true,
  },

<script setup>
import { useRoute } from "vue-router";
onBeforeMount(()=>{
    const route = useRoute();
    console.log(route.params, route.params.id); // 地址栏参数
    console.log("参数",route.params.id); // 这里是获取id 是因为在router里面定义的参数名字是id
})
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

巨蟹座守护骑士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值