Vue-59、Vue技术路由的props配置

路由的props配置

1、作用:让组件更方便的收到参数

routes:[
        {
            name:'guanyu',
            path:'/about',
            component:About
        },
        {
            path:'/home',
            component:Home,
            children:[
                {
                    path:'news',
                    component:News
                },
                {

                    path:'message',
                    component:Message,
                    children:[
                        {
                            name:'xiangqing',
                            path:'detail',
                            component:Detail,
                            //props的第一种写法,值为对象。该对象中的所有key-value都会以props形式传给detail组件
                            // props:{a:1,b:'hello'}

                            //props的第二种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的所有params参数,以props的形式detail组件
                            // props:true

                            //props的第三种写法,值为函数,该对象中的所有key-value都会以props形式传给detail组件
                            props($route){
                                return {id:$route.query.id,title:$route.query.title}
                            }
                        }
                    ]
                }
            ]
        },
    ]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用vue-element-admin的三级路由,需要进行以下步骤: 1. 在src/router/index.js文件中定义路由。 2. 在src/views/layout/components/Sidebar/index.vue文件中定义导航菜单。 3. 在src/views/layout/components/SidebarItem.vue文件中定义导航菜单子项。 下面是一个简单的示例: ```javascript // src/router/index.js import Layout from '@/layout' const routes = [ { path: '/dashboard', component: Layout, redirect: '/dashboard/index', children: [ { path: 'index', name: 'Dashboard', component: () => import('@/views/dashboard/index'), meta: { title: 'Dashboard', icon: 'dashboard' } } ] }, { path: '/example', component: Layout, redirect: '/example/table', name: 'Example', meta: { title: 'Example', icon: 'example' }, children: [ { path: 'table', name: 'Table', component: () => import('@/views/table/index'), meta: { title: 'Table', icon: 'table' } }, { path: 'tree', name: 'Tree', component: () => import('@/views/tree/index'), meta: { title: 'Tree', icon: 'tree' }, children: [ { path: 'list', name: 'TreeList', component: () => import('@/views/tree/list'), meta: { title: 'Tree List', icon: 'list' } }, { path: 'detail', name: 'TreeDetail', component: () => import('@/views/tree/detail'), meta: { title: 'Tree Detail', icon: 'detail' } } ] } ] } ] export default routes ``` ```html <!-- src/views/layout/components/Sidebar/index.vue --> <template> <div class="sidebar-container"> <el-scrollbar wrap-class="sidebar-wrapper"> <el-menu :default-active="$route.path" mode="vertical" :collapse="sidebar.opened" :unique-opened="true" :default-openeds="sidebar.openeds" class="el-menu-vertical-demo sidebar-menu" router collapse-transition > <sidebar-item v-for="item in sidebar.items" :key="item.index" :item="item" /> </el-menu> </el-scrollbar> </div> </template> <script> import SidebarItem from './SidebarItem' export default { name: 'Sidebar', components: { SidebarItem }, props: { sidebar: { type: Object, default: () => ({}) } } } </script> ``` ```html <!-- src/views/layout/components/SidebarItem.vue --> <template> <div> <template v-if="!item.children"> <el-menu-item :index="item.path" :route="{ path: item.path }"> <i :class="`iconfont ${item.meta.icon}`"></i> <span slot="title">{{ item.meta.title }}</span> </el-menu-item> </template> <template v-else> <el-submenu :index="item.path"> <template slot="title"> <i :class="`iconfont ${item.meta.icon}`"></i> <span>{{ item.meta.title }}</span> </template> <sidebar-item v-for="child in item.children" :key="child.path" :item="child" /> </el-submenu> </template> </div> </template> <script> import SidebarItem from './SidebarItem' export default { name: 'SidebarItem', components: { SidebarItem }, props: { item: { type: Object, default: () => ({}) } } } </script> ``` 在这个示例中,我们定义了一个路由配置,包含了两个一级路由,即/dashboard和/example,以及一个二级路由/example/tree,它包含了两个三级路由/example/tree/list和/example/tree/detail。然后在Sidebar组件中,我们使用了一个递归组件SidebarItem来渲染导航菜单,这个组件可以处理任意层级的菜单项。在SidebarItem组件中,我们根据是否存在子菜单来渲染el-menu-item或el-submenu,然后递归渲染子菜单。 使用vue-element-admin的三级路由需要注意以下几点: 1. 三级路由需要在二级路由的children属性中定义。 2. 导航菜单需要使用递归组件来渲染。 3. 递归渲染组件需要注意性能问题,可以使用v-for的key来优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一叶飘零晋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值