美文网首页
12-导航菜单的递归

12-导航菜单的递归

作者: 云桃桃 | 来源:发表于2019-08-23 17:26 被阅读0次
    • 父级
    <template>
      <div>
        <template v-if="onlyOne(item)">
          <el-menu-item :index="item.children[0].path">
            <span :class="['iconfont','icon-'+item.meta.icon]" />
            <span slot="title">{{ item.meta.title }}</span>
          </el-menu-item>
        </template>
        <template v-else>
          <el-submenu :index="item.path">
            <template slot="title">
              <span :class="['iconfont','icon-'+item.meta.icon]" />
              <span>{{ item.meta.title }}</span>
            </template>
            <InnerItem :key="item.path" :items="item.children" />
          </el-submenu>
    
        </template>
      </div>
    
    </template>
    
    <script>
    import InnerItem from './innerItem'
    export default {
        name: 'BarItem',
        components: {
            InnerItem
        },
        props: {
            item: {
                type: Object,
                default() {
                    return []
                }
            }
        },
        data() {
            return {
    
            }
        },
        methods: {
            onlyOne(item) {
                if (item.children && item.children.length === 1) {
                    return true
                }
                return false
            }
        }
    }
    </script>
    
    <style scoped>
    .iconfont{margin-right: 10px;}
    </style>
    
    
    
    • 子级递归
    <template>
      <div>
        <template v-for="child in items">
          <template v-if="onlyOne(child)">
            <el-menu-item :key="child.path" :index="child.path">
              <span slot="title">{{ child.meta.title }}</span>
            </el-menu-item>
          </template>
          <template v-else>
            <el-submenu :key="child.path" :index="child.path">
              <template slot="title">
                <span>{{ child.meta.title }}</span>
              </template>
             //  自己调用自己
              <InnerItem :key="child.path" :items="child.children" />
            </el-submenu>
          </template>
        </template></div>
    </template>
    
    <script>
    export default {
        name: 'InnerItem',
        components: {
    
        },
        props: ['items'],
        data() {
            return {
    
            }
        },
        methods: {
            onlyOne(item) {
                if (!item.children || (item.children && item.children.length === 1)) {
                    return true
                }
                return false
            }
        }
    }
    </script>
    <style scoped>
    </style>
    
    

    相关文章

      网友评论

          本文标题:12-导航菜单的递归

          本文链接:https://www.haomeiwen.com/subject/piitectx.html