当前位置:   article > 正文

vue动态面包屑导航_vue面包屑导航实现

vue面包屑导航实现

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

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

router.js路由文件:

  1. {
  2. path:'/index',
  3. name:'index',
  4. component:()=>import('@/views/home/index.vue'),
  5. meta:{
  6. title:'首页'
  7. },
  8. children:[
  9. {
  10. path:'/home',
  11. name:'home',
  12. component:()=>import('@/views/home/home.vue'),
  13. meta:{
  14. title:'home页'
  15. },
  16. children:[
  17. {
  18. path:'/second',
  19. name:'second',
  20. component:()=>import('@/views/home/second.vue'),
  21. meta:{
  22. title:'second'
  23. }
  24. }
  25. ]
  26. },
  27. ]
  28. },

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

自定义组件Bread.vue

  1. <template>
  2. <div>
  3. <el-breadcrumb separator="/">
  4. <el-breadcrumb-item v-for="item in breadlitst" :key="item">{{item}}</el-breadcrumb-item>
  5. </el-breadcrumb>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. name: "V2Bread",
  11. data() {
  12. return {
  13. breadlitst:[]
  14. };
  15. },
  16. mounted() {
  17. // console.log(this.$route)
  18. },
  19. methods: {},
  20. watch:{
  21. $route:{
  22. handler(route){
  23. let List = route.matched;
  24. List.forEach(item => {
  25. this.breadlitst.push(item.meta.title)
  26. });
  27. },immediate:true
  28. }
  29. }
  30. };
  31. </script>
  32. <style lang="scss" scoped></style>

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

  1. <template>
  2. <div id="app">
  3. <Bread />
  4. <router-view />
  5. </div>
  6. </template>
  7. <script>
  8. import Bread from '@/components/bread.vue'
  9. export default {
  10. data() {
  11. return {};
  12. },
  13. components:{
  14. Bread
  15. }
  16. };
  17. </script>
  18. <style></style>

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/412850?site
推荐阅读
相关标签
  

闽ICP备14008679号