<template>
  <span class="py-breadcrumb__item">
    <span :class="['py-breadcrumb__inner', to ? 'is-link': '']" ref="link" role="link">
      <slot></slot>
    </span>
    <i v-if="separatorClass" class="py-breadcrumb__separator" :class="separatorClass"></i>
    <span v-else class="py-breadcrumb__separator" role="presentation">{{separator}}</span>
  </span>
</template>

<script>
export default {
  name: "PyBreadcrumbItem",
  props: {
    to: {},
    replace: Boolean
  },
  data() {
    return {
      separator: "",
      separatorClass: ""
    };
  },
  inject: ["PyBreadcrumb"],

  mounted() {
    this.separator = this.PyBreadcrumb.separator;
    this.separatorClass = this.PyBreadcrumb.separatorClass;
    const link = this.$refs.link; // 获取span元素
    link.setAttribute("role", "link");
    link.addEventListener("click", _ => {
      const { to, $router } = this;
      if (!to || !$router) return;
      this.replace ? $router.replace(to) : $router.push(to);
    });
  }
};
</script>