Element-ui 路由导航

这篇具有很好参考价值的文章主要介绍了Element-ui 路由导航。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Element-ui 路由导航

1.router数组

const routes = [
  {
    path: '/', redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    components: {
      aside,
      default: main,
      header,
    },
    meta: {
      titel: '首页',
      icon: 'el-icon-location',
    },
  },
  {
    path: '/book',
    components: {
      aside,
      default: main,
      header,
    },
    meta: {
      icon: 'el-icon-location',
      titel: '预约管理',
    },
    children: [
      {
        path: 'reg1',
        components: {
          aside,
          default: main,
          header,
        },

        meta: {
          titel: '预约挂号',
          icon: 'el-icon-location',
        },
      },
      {
        path: 'reg2',
        components: {
          aside,
          default: main,
          header,
        },
        meta: {
          titel: '预约核酸检测',
          icon: 'el-icon-location',
        },
      },
      {
        path: 'reg3',
        components: {
          aside,
          default: main,
          header,
        },
        meta: {
          titel: '预约体检',
          icon: 'el-icon-location',
        },
      },
      {
        path: 'reg4',
        components: {
          aside,
          default: main,
          header,
        },
        meta: {
          titel: '预约排班',
          icon: 'el-icon-location',
        },
      },
    ],
  },
]

2.下载插件(解决div的bug)

下载:
npm install --save vue-fragment
引用:
import Fragment from 'vue-fragment'
Vue.use(Fragment.Plugin)

3.创建sidebar-item

<template>
  <fragment v-if="!item.hidden">
    <!-- 渲染菜单项 -->
    <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
        <el-menu-item :index="resolvePath(onlyOneChild.path)">
            <i :class="item.meta.icon"></i>
            <span slot="title">{{ item.meta.titel }}</span>
        </el-menu-item>
    </template>

    <!-- 渲染子菜单 -->
    <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
      <template slot="title">
           <i :class="item.meta.icon"></i>
            <span slot="title">{{ item.meta.titel }}</span>
      </template>
      <sidebar-item
        v-for="child in item.children"
        :key="child.path"
        :is-nest="true"
        :item="child"
        :base-path="resolvePath(child.path)"
        class="nest-menu"
      />
    </el-submenu>
  </fragment>
</template>

<script>
export default {
  name: 'SidebarItem',
  props: {
    // route object
    item: {
      type: Object,
      required: true,
    },
    isNest: {
      type: Boolean,
      default: false,
    },
    basePath: {
      type: String,
      default: '',
    },
  },
  data() {
    // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
    // TODO: refactor with render function
    this.onlyOneChild = null
    return {}
  },
  methods: {
    hasOneShowingChild(children = [], parent) {
      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
      }

      return false
    },
    resolvePath(routePath) {
      return routePath ? this.basePath + '/' + routePath : this.basePath
    },
  },
}
</script>

4.引用组件文章来源地址https://www.toymoban.com/news/detail-531362.html

  <SidebarItem v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />

到了这里,关于Element-ui 路由导航的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包