vue2实现面包屑功能

目录

1. store/index.js

2. router/index.js

3. Header.vue


在Vue 2中实现面包屑导航是一种常见的前端实践,它可以帮助用户了解当前页面在网站结构中的位置,并快速导航到上一级或根目录。以下是使用Vue 2实现面包屑导航的基本步骤:

1. store/index.js

state中定义一个面包屑名称:currentPathName

mutations中定义一个方法:

export default new Vuex.Store({
  state: {
    currentPathName: ''
  },
  getters: {
  },
  mutations: {
    setPath (state) {
      state.currentPathName = localStorage.getItem('currentPathName')
    }
  },
  actions: {
  },
  modules: {
  }
})

2. router/index.js

使用路由守卫获取路由名称

router.beforeEach((to, from, next) => {
  // 设置当前的路由名称,为了在Header组件中去使用
  localStorage.setItem('currentPathName', to.name)
  // 触发store的数据更新
  store.commit('setPath')
  // 其他的情况都放行
  next()
})

3. Header.vue

<!--其它代码-->
<el-breadcrumb-item><a href="">{{ currentPathName }}</a></el-breadcrumb-item>
<!--其它代码-->
computed: {
  currentPathName () {
    return this.$store.state.currentPathName
  }
}
<!--其它代码-->

相关推荐

  1. vue2实现面包屑功能

    2024-05-01 13:22:05       31 阅读
  2. vue2 + antvx6 实现流程图功能

    2024-05-01 13:22:05       26 阅读
  3. vue2实现记住密码功能

    2024-05-01 13:22:05       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-01 13:22:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 13:22:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 13:22:05       87 阅读
  4. Python语言-面向对象

    2024-05-01 13:22:05       96 阅读

热门阅读

  1. react中解决 Capture Value 问题

    2024-05-01 13:22:05       35 阅读
  2. FAISS原理和使用总结

    2024-05-01 13:22:05       34 阅读
  3. H.264码流解析

    2024-05-01 13:22:05       30 阅读
  4. Nacos和Eureka有什么区别

    2024-05-01 13:22:05       32 阅读
  5. 指代消解原理

    2024-05-01 13:22:05       28 阅读
  6. Day41 HTTP编程

    2024-05-01 13:22:05       32 阅读
  7. 邦芒面试:面试时,如何展现卓越的口才

    2024-05-01 13:22:05       30 阅读