btn-group.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="btn-group-with-icon">
  3. <i v-if="showIcon" :class="icon"></i>
  4. <a href="javascript:;" v-for="(btn, index) of btns" :key="btn" @click="click(index)"
  5. :class="{ active: activeIndex == index }">
  6. {{btn.text}}
  7. </a>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. // 名称
  13. name: "BtnGroup",
  14. // 传入参数
  15. props: {
  16. // 图标
  17. icon: String,
  18. // 按钮的数组
  19. /**
  20. * {
  21. text: "某某风场",
  22. }
  23. */
  24. btns: Array,
  25. // 默认选中第几个
  26. index: {
  27. type: Number,
  28. default: 0
  29. }
  30. },
  31. // 自定义事件
  32. emits: {
  33. // 选中事件
  34. select: null
  35. },
  36. // 数据
  37. data() {
  38. return {
  39. showIcon: false,
  40. activeIndex: 0,
  41. }
  42. },
  43. // 函数
  44. methods: {
  45. click: function(index) {
  46. this.activeIndex = index;
  47. this.$emit('select', index, this.btns[index]);
  48. }
  49. },
  50. // 生命周期钩子
  51. beforeCreate() {
  52. // 创建前
  53. },
  54. created() {
  55. // 创建后
  56. },
  57. beforeMount() {
  58. // 渲染前
  59. },
  60. mounted() {
  61. // 渲染后
  62. if (this.icon && this.icon != "") {
  63. this.showIcon = true;
  64. } else {
  65. this.showIcon = false;
  66. }
  67. this.activeIndex = this.index;
  68. },
  69. beforeUpdate() {
  70. // 数据更新前
  71. },
  72. updated() {
  73. // 数据更新后
  74. },
  75. }
  76. </script>
  77. <style lang="less">
  78. .btn-group-with-icon {
  79. display: flex;
  80. flex-direction: row;
  81. i {
  82. color: @gray;
  83. margin-right: 0.556vh;
  84. }
  85. a {
  86. text-decoration: none;
  87. color: @gray;
  88. margin-right: 0.278vh;
  89. border: 1px solid @gray;
  90. border-radius: 2px;
  91. width: 9.815vh;
  92. height: 2.13vh;
  93. line-height: 2.13vh;
  94. text-align: center;
  95. font-size: 1.111vh;
  96. &:last-child {
  97. margin-right: 0;
  98. }
  99. &.active {
  100. color: @write;
  101. background-color: @darkBlue;
  102. }
  103. }
  104. }
  105. </style>