doum
7 天以前 074bcb8394fab66ce531c219e1e7de7c142ff2d5
admin/src/components/common/Menu.vue
@@ -27,33 +27,50 @@
import { mapState } from 'vuex'
import MenuItems from './MenuItems'
import Scrollbar from './Scrollbar'
import { findMenuByUrl, getOpenMenuIndexes } from '@/utils/menu'
export default {
  name: 'Menu',
  components: { Scrollbar, MenuItems },
  computed: {
    ...mapState(['menuData']),
    // 选中的菜单index
    activeIndex() {
      let path = this.$route.path
      if (path.endsWith('/')) {
        path = path.substring(0, path.length - 1)
      }
      const menuConfig = this.__getMenuConfig(path, 'index', this.menuData.list)
      if (menuConfig == null) {
        return null
      } else {
        this.$store.commit('pushtags', menuConfig)
      }
      return menuConfig.index
    activeIndex () {
      const menuConfig = findMenuByUrl(this.$route.path, this.menuData.list)
      return menuConfig ? menuConfig.index : null
    },
    // 默认展开的菜单index
    defaultOpeneds() {
      // return this.menuData.list.map(menu => menu.index)
      return [this.menuData.list[0].index]
    defaultOpeneds () {
      const openeds = getOpenMenuIndexes(this.$route.path, this.menuData.list)
      if (openeds.length > 0) {
        return openeds
      }
      return this.menuData.list.length > 0 ? [this.menuData.list[0].index] : []
    }
  },
  watch: {
    '$route': {
      handler () {
        this.syncMenuState()
      },
      immediate: true
    }
  },
  methods: {
    syncMenuState () {
      const menuConfig = findMenuByUrl(this.$route.path, this.menuData.list)
      if (menuConfig) {
        this.$store.commit('pushtags', menuConfig)
      }
      this.$nextTick(() => {
        const menuRef = this.$refs.menu
        if (!menuRef) {
          return
        }
        getOpenMenuIndexes(this.$route.path, this.menuData.list).forEach(index => {
          menuRef.open(index)
        })
      })
    },
    // 处理菜单选中
    handleSelect(menuIndex) {
      const menuConfig = this.__getMenuConfig(menuIndex, 'index', this.menuData.list)