element 面包屑联动(vue3)

路由+面包屑联动

vite+vue3+elementplus+路由
基于路由的话,可以做到无论是点击菜单栏,还是点击面包屑,还是直接强行修改ulr,面包屑都能得到响应.

大概效果:
在这里插入图片描述

1 main.js + App.vue

main.js

import App from    './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')

App.vue:

App直接就是 header+aside+main+footer的布局
main = 面包屑 + 路由
在这里插入图片描述

2 路由配置

v1
____sub1
____sub2
____sub3
____sub4:{sub-1,sub-2}

v2
v3
v4

{
            path: '/v1',
            name: 'v1',
            //component: index,
            redirect:'/v1/sub1',// 进入/index的时候,重定向到/index/a1
            meta: {keepAlive: true},
            children: [  //使用嵌套路由
                {
                    path: 'sub1',
                    name: 'sub1',
                    component:moves2
                },
                {
                    path: 'sub2',
                    name: 'sub2',
                    component:moves1
                },
                {
                    path: 'sub3',
                    name: 'sub3',
                    component:moves2
                },
                {
                    path: 'sub4',
                    name: 'sub4',
                    redirect:'/v1/sub4/sub-1',
                    meta: {keepAlive: true},
                    children: [  //使用嵌套路由
                        {
                            path: 'sub-1',
                            name: 'sub-1',
                            component:moves1
                        }
                    ]
                },
            ]
        },
        {
            path: '/v2',
            name: 'v2',
            component: moves2,
            props:true,
        },
        {
            path: '/v3',
            name: 'v3',
            component: moves1,
            props:true,
        },
        {
            path: '/v4',
            name: 'v4',
            component: moves2,
            props:true,
        },

3 aside.vue

<template>
    <el-row class="tac">
      <el-col :span="24">
        <h5 class="mb-2">Custom colors</h5>
        <el-menu
          active-text-color="#ffd04b"
          background-color="#545c64"
          class="el-menu-vertical-demo"
          default-active="2"
          text-color="#fff"
          @open="handleOpen"
          @close="handleClose"
          @select="handleSelect"
        >
          <el-sub-menu index="v1">
            <template #title>
              <el-icon><location /></el-icon>
              <span>Navigator One</span>
            </template>
            <el-menu-item-group title="Group One">
              <el-menu-item index="sub1">item one</el-menu-item>
              <el-menu-item index="sub2">item two</el-menu-item>
            </el-menu-item-group>

            <el-menu-item-group title="Group Two">
              <el-menu-item index="sub3">item three</el-menu-item>
            </el-menu-item-group>

            <el-sub-menu index="sub4">
              <template #title>item four</template>
              <el-menu-item index="sub-1">item one</el-menu-item>
            </el-sub-menu>
          </el-sub-menu>

          <el-menu-item index="v2">
            <el-icon><document /></el-icon>
            <span>Navigator Two</span>
          </el-menu-item>
          <el-menu-item index="v3">
            <el-icon><document /></el-icon>
            <span>Navigator Three</span>
          </el-menu-item>
          <el-menu-item index="v4">
            <el-icon><setting /></el-icon>
            <span>Navigator Four</span>
          </el-menu-item>
        </el-menu>
      </el-col>
    </el-row>
  </template>
  
<script>
  import {  getCurrentInstance } from "vue";
  //const { proxy } = getCurrentInstance();

  import {
    Document,
    Menu as IconMenu,
    Location,
    Setting,
  } from '@element-plus/icons-vue'

  
  export default{
        setup(){
            const handleClose2 = (key, keyPath) => {
                console.log(key, keyPath)
            }
            const  handleOpen2 = (key, keyPath) => {
                    console.log(key, keyPath)
                }
                return {handleClose2,handleOpen2 }
    },
  methods: {
    handleOpen(key, keyPath) {
        console.log("methods open",key, keyPath)
    },
    handleClose(key, keyPath){
        console.log("methods close",key, keyPath)
    },
    handleSelect(key, keyPath){
        let fullpath = '/'
        let i = 0
        for (i = 0; i < keyPath.length; i++) { 
            fullpath += keyPath[i] + "/";
        }
        console.log("methods select",key, keyPath,fullpath)
        this.$router.push(fullpath)
    },
  }
}
  </script>

核心: 当点击菜单栏的时候,页面跳转
在handleSelect():里面进行路由跳转

4 面包屑

<template>
    <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/v1/sub1' }">home</el-breadcrumb-item>
      <el-breadcrumb-item :to="item.path" v-for="(item) in List.data" :key="item.path">
        {{ item.name }}
      </el-breadcrumb-item>

    </el-breadcrumb>
</template>

<script setup>
import { ArrowRight } from '@element-plus/icons-vue'
import { useRoute } from 'vue-router'
import { ref ,computed,reactive} from "vue";

const List = reactive({data:[]})
const route = useRoute()
List.data = computed(() => route.matched)

</script>

1.computed() 功能:关注的东西一旦发生变化,就会调用该函数.
页面的跳转都是经过路由的,route.matched记录了当前路由信息(跳转之后的).
2. reactive() 响应式数据.
route.matched 变 =>List.data变 =>页面响应

无论是点击菜单栏,还是点击面包屑,还是直接强行修改ulr,面包屑都能得到响应.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 中实现面包屑导航的联动可以通过使用路由钩子和动态路由参数来实现。 首先,你需要在 Vue Router 中设置路由参数。在定义路由时,可以使用冒号(:)来定义动态参数。例如: ```javascript const routes = [ { path: '/category/:categoryId', component: Category }, { path: '/category/:categoryId/product/:productId', component: Product }, ]; ``` 然后,在组件中,你可以使用 `$route.params` 访问路由参数。在面包屑导航组件中,你可以监听 `$route` 的变化,并根据当前路由参数来动态生成面包屑导航。 下面是一个简单的示例: ```vue <template> <div> <ul> <li v-for="crumb in breadcrumbs" :key="crumb.path"> <router-link :to="crumb.path">{{ crumb.label }}</router-link> </li> </ul> </div> </template> <script> export default { computed: { breadcrumbs() { const breadcrumbs = []; const { matched } = this.$route; matched.forEach(route => { const path = route.path; const label = route.meta && route.meta.breadcrumbLabel; if (label) { breadcrumbs.push({ path, label }); } }); return breadcrumbs; } } } </script> ``` 在上面的示例中,我们通过监听 `$route` 的变化,在 `breadcrumbs` 计算属性中根据当前路由的 `matched` 属性来生成面包屑导航的数据。每个路由都可以设置 `meta` 字段来定义面包屑导航的标签。 在路由的定义中,你可以像下面这样设置 `meta` 字段: ```javascript const routes = [ { path: '/category/:categoryId', component: Category, meta: { breadcrumbLabel: 'Category' } }, { path: '/category/:categoryId/product/:productId', component: Product, meta: { breadcrumbLabel: 'Product' } } ]; ``` 这样,当路由发生变化时,面包屑导航组件会根据当前路由的 `matched` 属性动态生成面包屑导航的数据。你可以在模板中使用 `v-for` 指令来渲染面包屑导航的每个项,并使用 `router-link` 组件来生成链接。 希望这个示例对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值