新文章 网摘 文章 随笔 日记

自己写的Vue动态加载菜单

<template>
  <ul :id="id">
    <li v-for="menu in menus" :key="menu.id">
      <a
        v-if="isNonExecutiveMenu(menu) && !hasChildren(menu)"
        href="#"
        style="color: rgb(193, 197, 210)"
        >{{ menu.Name }}</a
      >
      <a v-else-if="isNonExecutiveMenu(menu) && hasChildren(menu)" href="#">{{
        menu.Name
      }}</a>
      <a v-else-if="onlyHasController(menu)" :href="defaultUrl(menu)">{{
        menu.Name
      }}</a>
      <a v-else-if="isExternalUrl(menu)" @click="openUrl(menu.ExternalUrl)">{{
        menu.Name
      }}</a>
      <a v-else :href="contractUrl(menu)">{{ menu.Name }}</a>

      <MenuTree v-if="hasChildren(menu)" :menus="menu.MenuChildren"></MenuTree>
    </li>
  </ul>
</template>


<script>
import { mapState } from "vuex";

export default {
  name: "MenuTree",
  props: {
    id: null,
    menus: null,
  },
  computed: {},
  methods: {
    //非执行性菜单
    isNonExecutiveMenu: function (menu) {
      var result =
        this.$stringHelper.isNullOrWhiteSpace(menu.Action) &&
        this.$stringHelper.isNullOrWhiteSpace(menu.Controller) &&
        this.$stringHelper.isNullOrWhiteSpace(menu.ExternalUrl);

      return result;
    },
    //仅Controler非空
    onlyHasController: function (menu) {
      var result =
        this.$stringHelper.isNullOrWhiteSpace(menu.Action) &&
        this.$stringHelper.isNullOrWhiteSpace(menu.Controller) &&
        this.$stringHelper.isNullOrWhiteSpace(menu.ExternalUrl);

      return result;
    },
    //是否外部网址
    isExternalUrl: function (menu) {
      var result = !this.$stringHelper.isNullOrWhiteSpace(menu.ExternalUrl);

      return result;
    },
    //有子菜单
    hasChildren: function (menu) {
      return menu.MenuChildren.length > 0;
    },
    //打开网址
    openUrl: function (url) {
      //this.$router.push("/");
      this.$router.push(url);
      return false;
    },
    contractUrl: function (menu) {
      return "/" + menu.Controller + "/" + menu.Action;
    },
    defaultUrl: function (menu) {
      return "/" + menu.Controller + "/" + menu.DefaultAction;
    },
  },
};
</script>


<style lang="scss">
body {
  margin: 0;
  padding: 0;
}

.example {
  background: #fff;
  width: 770px;
  height: 570px;
  border: 1px #000 solid;
  margin: 20px auto;
  padding: 15px;
  border-radius: 3px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
}

/* main menu styles */
#nav {
  display: inline-block;
  width: 100%;
  height: 31px;
  margin: 0px auto;
  padding: 0;
  /*background: #F8F8F8;*/
  /*background-image: url('images/bar.png') ;*/
  -ms-background-repeat: repeat;
  background-repeat: repeat;
  /*border-bottom: solid thin rgb(227, 228, 230);*/
  background-image: url("~@/assets/images/back2.jpg");

  position: fixed;
  top: 0;
  left: 0;
  background-color: #eef3fa;
  z-index: 99999;
  font-size: 12px;
  background-size: 100% 100%;
  height: 40px;
  padding-top: 15px;
}

#nav li {
  /*margin: 10px;*/
  float: left;
  position: relative;
  list-style: none;
  height: 25px;
  line-height: 25px;
  text-align: left;
}

#nav a {
  font-size: 12px;
  color: #000000;
  text-decoration: none;
  display: block;
  padding: 0px 20px;
}

/* selected menu element */
#nav .current a,
#nav li:hover > a {
  background: #b3d7f3;
  color: #000000;
  border: #284155 thin solid;
}

/* sublevels */
#nav ul li:hover a,
#nav li:hover li a {
  background: none;
  border: none;
  color: #000;
}

#nav ul li a:hover {
  background: #b3d7f3;
  color: #000000;
  border: #284155 thin solid;
  border-radius: 3px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
}

#nav ul li:first-child > a {
  -moz-border-radius-topleft: 3px; /*some css3*/
  -moz-border-radius-topright: 3px;
  -webkit-border-top-left-radius: 3px;
  -webkit-border-top-right-radius: 3px;
}

#nav ul li:last-child > a {
  -moz-border-radius-bottomleft: 3px; /*some css3*/
  -moz-border-radius-bottomright: 3px;
  -webkit-border-bottom-left-radius: 3px;
  -webkit-border-bottom-right-radius: 3px;
}

/* drop down */
#nav li:hover > ul {
  opacity: 1;
  visibility: visible;
}

#nav ul {
  opacity: 0;
  visibility: hidden;
  padding: 0;
  width: 175px;
  position: absolute;
  background: #ffffff /*url(/jscss/demoimg/201305/bg.png) repeat-x 0 0*/;
  border-top: thin solid #eeeeee;
  border-left: thin solid #eeeeee;
  border-right: 1px solid #b9b9b9;
  border-bottom: 1px solid #b9b9b9;

  border-bottom-right-radius: 2px;
  border-bottom-left-radius: 2px;
  -moz-border-radius-bottomright: 2px; /*右下角*/
  -moz-border-radius-bottomleft: 2px; /*左下角*/
  -webkit-border-bottom-right-radius: 2px;
  -webkit-border-bottom-left-radius: 2px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  -webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  -moz-transition: opacity 0.25s linear, visibility 0.1s linear 0.1s;
  -webkit-transition: opacity 0.25s linear, visibility 0.1s linear 0.1s;
  -o-transition: opacity 0.25s linear, visibility 0.1s linear 0.1s;
  transition: opacity 0.25s linear, visibility 0.1s linear 0.1s;
  z-index: 9999;
}

#nav ul li {
  float: none;
  margin: 0;
  height: 30px;
  line-height: 30px;
}

#nav ul a {
  font-weight: normal;
  font-size: 12px;
}

#nav ul ul {
  left: 160px;
  top: 0px;
}
.disable {
  color: red;
}
</style>

 

posted @ 2020-12-24 15:33  岭南春  阅读(164)  评论(0)    收藏  举报