效果图:

 找到这两个页面:

 我是只有一个页面(动态的路由)加:

 

 样式:

 

<template>
  <div v-if="!item.hidden">
    <template
      v-if="
        hasOneShowingChild(item.children, item) &&
        (!onlyOneChild.children || onlyOneChild.noShowingChildren) &&
        !item.alwaysShow
      "
    >
      <app-link
        v-if="onlyOneChild.meta"
        :to="resolvePath(onlyOneChild.path, onlyOneChild.query)"
      >
        <el-menu-item
          :index="resolvePath(onlyOneChild.path)"
          :class="{ 'submenu-title-noDropdown': !isNest }"
        >
          <item
            :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
            :title="onlyOneChild.meta.title"
            :count="item.meta.title === '出库任务调度' ? count : 0"
          />
        </el-menu-item>
      </app-link>
    </template>

    <el-submenu
      v-else
      ref="subMenu"
      :index="resolvePath(item.path)"
      popper-append-to-body
    >
      <template slot="title">
        <item
          v-if="item.meta"
          :icon="item.meta && item.meta.icon"
          :title="item.meta.title"
        />
      </template>
      <sidebar-item
        v-for="(child, index) in item.children"
        :key="index"
        :is-nest="true"
        :item="child"
        :base-path="resolvePath(child.path)"
        class="nest-menu"
      />
    </el-submenu>
  </div>
</template>

<script>
import path from "path";
import { isExternal } from "@/utils/validate";
import Item from "./Item";
import AppLink from "./Link";
import FixiOSBug from "./FixiOSBug";
import {outOrderList} from "@/api/exWarehouse/dispatchDistribute";

export default {
  name: "SidebarItem",
  components: { Item, AppLink },
  mixins: [FixiOSBug],
  props: {
    // route object
    item: {
      type: Object,
      required: true,
    },
    isNest: {
      type: Boolean,
      default: false,
    },
    basePath: {
      type: String,
      default: "",
    },
  },
  data() {
    this.onlyOneChild = null;
    return {
      count:null
    };
  },
  mounted() {
    // console.log(this.item, "itemitem");
    this.metaCount(this.onlyOneChild.meta);
  },
  methods: {
    metaCount(data) {
      if (data.title == '出库任务调度') {//判断自己需要传入的标题,不然你标题多的时候会请求很多次
        outOrderList().then(res=>{
          this.count = res.total;
        })
      }
    },
    hasOneShowingChild(children = [], parent) {
      if (!children) {
        children = [];
      }
      const showingChildren = children.filter((item) => {
        if (item.hidden) {
          return false;
        } else {
          // Temp set(will be used if only has one showing child)
          this.onlyOneChild = item;
          return true;
        }
      });

      // When there is only one child router, the child router is displayed by default
      if (showingChildren.length === 1) {
        return true;
      }

      // Show parent if there are no child router to display
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
        return true;
      }
      // console.log(this.onlyOneChild, "this.onlyOneChild");
      return false;
    },
    resolvePath(routePath, routeQuery) {
      if (isExternal(routePath)) {
        return routePath;
      }
      if (isExternal(this.basePath)) {
        return this.basePath;
      }
      if (routeQuery) {
        let query = JSON.parse(routeQuery);
        return { path: path.resolve(this.basePath, routePath), query: query };
      }
      return path.resolve(this.basePath, routePath);
    },
  },
};
</script>

 

<template>
  <div class="item-sty">
    <svg-icon :icon-class="icon" />
    <span slot="title" :title="title">{{ title }}</span>
    <!-- 计数标记 -->
    <span v-if="count > 0" class="count-badge">{{ count }}</span>
  </div>
</template>

<script>
export default {
  name: "MenuItem",
  props: {
    icon: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "",
    },
    count: {  // 新增此行
      type: Number,
      default: 0
    },
  },
};
</script>

<style scoped>
.item-sty {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  position: relative; /* 为了绝对定位计数标记 */
}

.count-badge {
  width: 25px;
  height: 20px;
  line-height: 19px;
  position: absolute;
  top: 8px;
  right: 5px;
  background-color: red;
  color: white;
  border-radius: 9px;
  font-size: 12px;
  text-align: center;
  border: 1px solid;
}
</style>

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐