BreadcrumbItem.vue 1.6 KB
Newer Older
陈文彬 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<template>
  <span class="breadcrumb__item">
    <span ref="linkRef" :class="['breadcrumb__inner', to || isLink ? 'is-link' : '']">
      <slot />
    </span>
    <i v-if="separatorClass" class="breadcrumb__separator" :class="separatorClass"></i>
    <span v-else class="breadcrumb__separator">{{ separator }}</span>
  </span>
</template>

<script lang="ts">
  import { defineComponent, inject, ref, onMounted, unref } from 'vue';
  import { useRouter } from 'vue-router';
14
  import { useEventListener } from '/@/hooks/event/useEventListener';
陈文彬 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

  export default defineComponent({
    name: 'BreadcrumbItem',
    props: {
      to: {
        type: [String, Object],
        default: '',
      },
      replace: {
        type: Boolean,
        default: false,
      },
      isLink: {
        type: Boolean,
        default: false,
      },
    },
    setup(props) {
      const linkRef = ref<Nullable<HTMLElement>>(null);
V
vben 已提交
34

陈文彬 已提交
35 36 37 38
      const parent = inject('breadcrumb') as {
        separator: string;
        separatorClass: string;
      };
V
vben 已提交
39

陈文彬 已提交
40 41 42 43 44
      const { push, replace } = useRouter();

      onMounted(() => {
        const link = unref(linkRef);
        if (!link) return;
45
        useEventListener({
陈文彬 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
          el: link,
          listener: () => {
            const { to } = props;
            if (!props.to) return;
            props.replace ? replace(to) : push(to);
          },
          name: 'click',
          wait: 0,
        });
      });

      return {
        linkRef,
        separator: parent.separator && parent.separator,
        separatorClass: parent.separatorClass && parent.separatorClass,
      };
    },
  });
</script>