breadcrumb.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div id="breadcrumb">
  3. <el-tag v-for="(item, index) in list" :key="index" type="primary" size="small" closable class="tags" @close="closeTag(index)">{{ item.name }}</el-tag>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'breadcrumb',
  9. props: {},
  10. components: {},
  11. data: () => ({
  12. list: [],
  13. }),
  14. created() {
  15. this.getList();
  16. },
  17. computed: {},
  18. methods: {
  19. getList() {
  20. console.log('bread getlist');
  21. //将面包屑列表存到sessionStorage中,存为list,{name:xxx,path:xxx}形式
  22. //1)是从sessionStorage中取出之前的列表
  23. let o_list = sessionStorage.getItem('bread');
  24. if (o_list !== null) {
  25. //如果之前有浏览记录,那就将值取出来
  26. this.$set(this, `list`, JSON.parse(o_list));
  27. }
  28. //2)查询当前路由是否已经存在
  29. let { name, path } = this.$route;
  30. if (path === '/admin' || path === '/admin/index') return;
  31. let object = { name: name, path: path };
  32. let res = [];
  33. //列表有值,过滤看看有没有当前路由
  34. if (this.list.length > 0) {
  35. res = this.list.filter(fil => fil.path === object.path);
  36. if (res.length <= 0) {
  37. //没有当前路由,加上
  38. this.list.push(object);
  39. sessionStorage.setItem(`bread`, JSON.stringify(this.list));
  40. }
  41. } else {
  42. //列表没有值,直接加上
  43. this.list.push(object);
  44. sessionStorage.setItem(`bread`, JSON.stringify(this.list));
  45. }
  46. },
  47. closeTag(index) {
  48. let elseList = this.list.filter((fil, filIndex) => filIndex !== index);
  49. this.$set(this, `list`, elseList);
  50. sessionStorage.setItem('bread', JSON.stringify(elseList));
  51. if (this.list.length > 0) this.$router.push(this.list[this.list.length - 1].path);
  52. else this.$router.push({ path: '/admin/index' });
  53. },
  54. },
  55. };
  56. </script>
  57. <style lang="less" scoped>
  58. .tags {
  59. margin: 0 0.3rem;
  60. }
  61. </style>