layout布局导航菜单

import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
Vue.use(Router)
export default new Router({
  routes: [
    {
      path: '/',
      component: Layout,
      redirect: '/test',
      children: [
        {
          path: 'test',
          name: 'test',
          component: () => import('@/view/test'),
          meta: { title: '首页', icon: 'home' }
        }
      ]
    },

    {
      path: '/s',
      component: Layout,
      redirect: '/s/map',
      meta: { title: '其他', icon: 'home' },
      children: [
        {
          path: 'map',
          name: 'map',
          component: () => import('@/components/map'),
          meta: { title: '地图', icon: 'home' },
        },
        {
          path: 'gojs',
          name: 'gojs',
          component: () => import('@/view/gojs'),
          meta: { title: 'go', icon: 'home' },
        },
      ]
    },
    {
      path: '/home',
      component: Layout,
      redirect: '/home/homes',
      children:[
        {
          path:'homes',
          name:'homes',
          component: () => import('@/view/home'),
          meta: { title: '图表', icon: 'home' }
        }
      ]
    },
  ],
  mode: "history"    //去掉/#/
})

 

children 里面的path 应该要加上父级的路径 ,这里没加 后面统一拼上了

Menu组件

<template>
  <Menu theme="dark" accordion width='220px'>
    <Item v-for="route in routes" :key="route.path" :item="route"></Item>
  </Menu>
</template>
<script>
import Item from './item.vue'
export default {
  name: 'm',
  components:{
    Item
  },
  computed: {
    routes() {
      return this.$router.options.routes
    },
  },
  created() {
    console.log('menu',this.$router.options.routes)
  },
  methods: {

  }
}
</script>

<style scoped>
</style>

Item组件

<template>
  <div>
    <MenuItem v-if="hasOneChild(item)" :to="item.path" :name="item.path">
      <Icon type="ios-paper" /><span>{{item.children[0].meta.title}}</span>
    </MenuItem>
    <template v-else>
        <Submenu :name="item.path">
          <template slot="title">
            <Icon type="ios-apps" /><span>{{item.meta.title}}</span>
          </template>  
          <MenuItem  v-for="items in item.children" :key="items.path" :to="resolvePath(item.path,items.path)" :name="resolvePath(item.path,items.path)">
            <Icon type="ios-paper" /><span>{{items.meta.title}}</span>
          </MenuItem>
        </Submenu> 
    </template>

  </div>

</template>
<script>
import path from 'path'
export default {
  name: 'item',
  computed: {

  },
  props:{
    item:{
      type:Object,
      required:true
    }
  },

  created() {
    console.log('item',this.item)
  },
  methods: {
    // 如果children 里面只有一个 就按照单个显示  如果里面有1个以上就按组折叠显示
    hasOneChild(item){
      if(item.children && item.children.length==1){
        return true
      }else{
        return false
      }
    },
    // chilidren 里面的路径 拼上父级路径
    resolvePath(base,routePath){
       return path.resolve(base, routePath)
    }
  }
}
</script>

<style scoped>
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值