温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在vue中使用面包屑导航组件

发布时间:2021-02-23 17:28:00 来源:亿速云 阅读:254 作者:Leah 栏目:web开发

本篇文章为大家展示了如何在vue中使用面包屑导航组件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

vue的面包屑导航组件

用来将其放到navbar中;

Breadcrumb/index.vue

<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
   <transition-group>
    <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path" v-if="item.meta.title">
     <span  v-if='item.redirect==="noredirect"||index==levelList.length-1' class="no-redirect">{{item.meta.title}}</span>
     <router-link  v-else :to="item.redirect||item.path">{{item.meta.title}}</router-link>
    </el-breadcrumb-item>
   </transition-group>
  </el-breadcrumb>
</template>
<script>
  export default {
    name: "idnex",
   data(){
     return {
      levelList:null
     }
   },
   created() {
    this.getBreadcrumb()
   },
   watch:{
    $route(){
     this.getBreadcrumb()
    }
   },
   methods:{
    getBreadcrumb(){
      let matched=this.$route.matched.filter(item=>item.name)//$route.matched 将会是一个包含从上到下的所有对象 (副本)。
      const first=matched[0]
      if(first && first.name !=='dashboard'){//$route.name当前路由名称 ;$route.redirectedFrom重定向来源的路由的名字
       matched=[{ path: '/dashboard', meta: { title: 'dashboard' }}].concat(matched)
      }
      this.levelList=matched
     }
   }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
 .app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 10px;
  .no-redirect {
   color: #97a8be;
   cursor: text;
  }
 }
</style>

ps:下面在看下一段代码Vue 面包屑导航

样式采用的是element ui 中的面包屑设置的,

<template>
<el-breadcrumb>
<el-breadcrumb-item separator = '/' v-for = "(item,index) in breadlist" :key = 'index' :to="{path: item.path}">{{item.meta.CName}}
</el-breadcrumb-item> 
</el-breadcrumb>
</template>
js部分
<script>
export default {
data(){
return {
breadlist: ''
}
},
created() {
this.getBread();
},
methods:{
getBread(){
this.breadlist = this.$route.matched;
this.$route.matched.forEach((item,index)=>{
//先判断父级路由是否是空字符串或者meta是否为首页,直接复写路径到根路径
item.meta.CName === '首页' ? item.path = '/' : this.$route.path === item.path;
});
}
},
watch:{
$route(){
this.getBread();
}
}
}
</script>

上述内容就是如何在vue中使用面包屑导航组件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI