menu-item.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <el-submenu :index="index" v-if="hasChildren">
  3. <template slot="title">
  4. <i :class="iconCls" v-if="hasIcon"></i>
  5. <span slot="title" @click.prevent="menuClick">{{ title }}</span>
  6. </template>
  7. <naf-menu-item
  8. v-for="(item, idx) in children"
  9. :key="idx"
  10. :index="index + '-' + idx"
  11. :title="item.title"
  12. :children="item.children"
  13. :options="item.options"
  14. :prefix="prefix"
  15. @naf-menu-item="$emit('naf-menu-item', $event)"
  16. >
  17. </naf-menu-item>
  18. </el-submenu>
  19. <el-menu-item :index="index" @click="menuClick" v-else
  20. ><i :class="iconCls"></i>
  21. <span slot="title" v-if="title.length < 10 && !hasTooltip">{{ title }}</span>
  22. <el-tooltip slot="title" v-else :content="hasTooltip ? options.tooltip : title" placement="top" effect="light">
  23. <span>{{ title.length > 9 ? title.substr(0, 9) + '...' : title }}</span>
  24. </el-tooltip>
  25. </el-menu-item>
  26. </template>
  27. <script>
  28. export default {
  29. name: 'naf-menu-item',
  30. props: {
  31. title: String,
  32. index: String,
  33. options: Object,
  34. children: Array,
  35. prefix: { type: String, default: '' },
  36. },
  37. mounted() {
  38. let _this = this;
  39. //这里可放到全局,提供给子 iframe 调用
  40. window.menu = function(to) {
  41. _this.$router.push(to);
  42. };
  43. },
  44. methods: {
  45. menuClick() {
  46. console.debug('click menu item....');
  47. if (this.options.url) {
  48. window.open(this.options.url, this.options.target);
  49. } else if (this.options.path) {
  50. const to = `${this.prefix}${this.options.path}`;
  51. if (to != (this.$route && this.$route.path)) {
  52. this.$router.push(to);
  53. } else {
  54. this.$emit('naf-menu-item', this.options);
  55. }
  56. }
  57. },
  58. },
  59. computed: {
  60. hasChildren() {
  61. return this.children && this.children.length > 0;
  62. },
  63. hasIcon() {
  64. return this.options.icon && this.options.icon.length > 0;
  65. },
  66. hasTooltip() {
  67. return this.options.tooltip && this.options.tooltip.length > 0;
  68. },
  69. iconCls() {
  70. if (this.options.icon && !(this.options.icon.indexOf('el-') === 0)) {
  71. return `naf-icons naf-icon-${this.options.icon}`;
  72. }
  73. return this.options.icon;
  74. },
  75. },
  76. };
  77. </script>