RuoYi
2021-04-14 cc50224d90b3a71b8cbab892faaa4ff82f0d9399
ruoyi-ui/src/components/TopNav/index.vue
@@ -30,6 +30,9 @@
<script>
import { constantRoutes } from "@/router";
// 不需要激活的路由
const noactiveList = ["/user/profile", "/dict/type", "/gen/edit", "/job/log"];
export default {
  data() {
    return {
@@ -42,10 +45,13 @@
  computed: {
    // 顶部显示菜单
    topMenus() {
      return this.routers.map((menu) => ({
        ...menu,
        children: undefined,
      }));
      let topMenus = [];
      this.routers.map((menu) => {
        if (menu.hidden !== true) {
          topMenus.push(menu);
        }
      });
      return topMenus;
    },
    // 所有的路由信息
    routers() {
@@ -69,6 +75,12 @@
    activeMenu() {
      const path = this.$route.path;
      let activePath = this.routers[0].path;
      var noactive = noactiveList.some(function (item) {
        return path.indexOf(item) !== -1;
      });
      if (noactive) {
        return;
      }
      if (path.lastIndexOf("/") > 0) {
        const tmpPath = path.substring(1, path.length);
        activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
@@ -83,21 +95,20 @@
      return activePath;
    },
  },
  beforeMount() {
    window.addEventListener('resize', this.setVisibleNumber)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setVisibleNumber)
  },
  mounted() {
    this.setVisibleNumber();
  },
  methods: {
    // 根据宽度计算设置显示栏数
    setVisibleNumber() {
      const width = document.body.getBoundingClientRect().width - 200;
      const elWidth = this.$el.getBoundingClientRect().width;
      const menuItemNodes = this.$el.children;
      const menuWidth = Array.from(menuItemNodes).map(
        (i) => i.getBoundingClientRect().width
      );
      this.visibleNumber = (
        parseInt(width - elWidth) / parseInt(menuWidth)
      ).toFixed(0);
      const width = document.body.getBoundingClientRect().width / 3;
      this.visibleNumber = parseInt(width / 85);
    },
    // 菜单选择事件
    handleSelect(key, keyPath) {
@@ -119,7 +130,7 @@
        });
      }
      this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
    },
    }
  },
};
</script>