vue动态面包屑导航

说明:根据不同的页面动态生成不同的面包屑导航

(不用在每个页面写死面包屑)

router.js路由文件:

{
    path:'/index',
    name:'index',
    component:()=>import('@/views/home/index.vue'),
    meta:{
      title:'首页'
    },
    children:[
      {
        path:'/home',
        name:'home',
        component:()=>import('@/views/home/home.vue'),
        meta:{
          title:'home页'
        },
        children:[
          {
            path:'/second',
            name:'second',
            component:()=>import('@/views/home/second.vue'),
            meta:{
              title:'second'
            }
          }
        ]
      },
     
    ]
  },

给每个路由添加元信息meta ,定义属性title(面包屑的名称)

自定义组件Bread.vue

<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item v-for="item in breadlitst" :key="item">{{item}}</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "V2Bread",

  data() {
    return {
        breadlitst:[]
    };
  },

  mounted() {
    // console.log(this.$route)
  },

  methods: {},
  watch:{
    $route:{
        handler(route){
            let List = route.matched;
            List.forEach(item => {
                this.breadlitst.push(item.meta.title)
            });
        },immediate:true
    }
  }
};
</script>

<style lang="scss" scoped></style>

监听route , 可以拿到meta的信息,放到数组中 遍历面包屑

<template>
  <div id="app">
    <Bread />
    <router-view />
  </div>
</template>
<script>
import Bread from '@/components/bread.vue'
export default {
  data() {
    return {};
  },
  components:{
    Bread
  }
};
</script>
<style></style>

把面包屑组件放到router-view前即可 这样每个页面都可访问到这个组件 并且是动态生成;

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值