随笔 - 142  文章 - 14  评论 - 0  阅读 - 70338

学习- vue4.x-router 带参数动态路由知识点理解demo

app.use(store).use(router).mount('#app')

通过调用 app.use(router),可以在任意组件中以 this.$router 的形式访问,并且以 this.$route 的形式访问当前路由

1、带参数的动态路由匹配

  使用场景:页面跳转,想要携带id到目标页面中使用推荐使用

代码演示:router/index.js

复制代码
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import testRouter from '../views/test-router.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  // 带参数的动态路由匹配
  {
    path: '/test/:id',
    name: 'Test',
    component: testRouter
  }
]

// routes传递给createRouter
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router
复制代码
test-router组件:
复制代码
<template>
  <div>test-router</div>
</template>

<script>
export default {
  name: ' testRouter'

}
</script>

<style>

</style>
复制代码
获取参数:this.$route.params.id

 

 

  

posted on   法老的微笑  阅读(188)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示