Vue 封装一个折叠面板组件

女爷i 2022-08-28 14:51 542阅读 0赞

该组件使用了 Element 的一些 icon 图标,以及 过渡动画 el-collapse-transition,需安装 element
在这里插入图片描述
具体使用方法,主要知识点 provide ,inject,this.$children 和 _uid (vue中无论递归组件,还是自定义组件,每个组件都有唯一的_uid)

  1. <!-- 折叠组件 -->
  2. <navigation-bar v-model="barName" accordion>
  3. <navigation-bar-item label="测试1" name="1" line>测试1</navigation-bar-item>
  4. <navigation-bar-item label="测试2" name="2" line>测试2</navigation-bar-item>
  5. <navigation-bar-item label="测试3" name="3">测试3</navigation-bar-item>
  6. </navigation-bar>

navigation-bar 组件代码如下

  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "navigationBar",
  9. provide () {
  10. return {
  11. Bar: this
  12. }
  13. },
  14. props: {
  15. value: {
  16. type: String,
  17. default: ''
  18. },
  19. accordion: {
  20. type: Boolean,
  21. default: false
  22. }
  23. },
  24. methods: {
  25. changeState (id) {
  26. this.$children.forEach(item => {
  27. if (item._uid !== id) {
  28. item.isShow = false
  29. } else {
  30. item.isShow = !item.isShow
  31. }
  32. })
  33. }
  34. },
  35. }
  36. </script>
  37. <style scoped>
  38. </style>

navigation-bar-item 组件代码如下,el-image 的图片地址使用的本地图片,请更换自己的路径

  1. <template>
  2. <div :class="line && !isShow ? 'content' : ''">
  3. <div class="navigationBar" @click="foldClick">
  4. <div class="navigationBarLeft"></div>
  5. <div class="navigationBarRight">
  6. <span>{ { label}}</span>
  7. <div class="navigationBarIcon">
  8. <el-image style="width: 18px; height: 18px" :src="require('../assets/img/doubt.png')" ></el-image>
  9. <i :class="isShow ? 'rotate' : 'rotate1'" ref="foldIcon" style="margin-left: 10px" class="el-icon-arrow-down"></i>
  10. </div>
  11. </div>
  12. </div>
  13. <el-collapse-transition>
  14. <div v-show="isShow">
  15. <slot></slot>
  16. </div>
  17. </el-collapse-transition>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: "navigationBarItem",
  23. inject: ['Bar'],
  24. props:{
  25. label:{
  26. type: String,
  27. required: true
  28. },
  29. name:{
  30. type: String,
  31. default: ''
  32. },
  33. line: {
  34. type: Boolean,
  35. default: false
  36. }
  37. },
  38. data() {
  39. return {
  40. isShow: false
  41. }
  42. },
  43. mounted() {
  44. // 默认展开
  45. this.Bar.value === this.name ? this.isShow = true : ''
  46. },
  47. methods: {
  48. // 导航条折叠
  49. foldClick() {
  50. if (this.Bar.accordion) {
  51. this.Bar.changeState(this._uid)
  52. } else {
  53. this.isShow = !this.isShow;
  54. }
  55. }
  56. }
  57. }
  58. </script>
  59. <style scoped>
  60. .navigationBar {
  61. display: flex;
  62. }
  63. .navigationBar:hover {
  64. cursor: pointer;
  65. }
  66. .navigationBarLeft {
  67. width: 6px;
  68. height: 25px;
  69. background-color: #3179F4;
  70. }
  71. .navigationBarRight {
  72. flex: 1;
  73. display: flex;
  74. height: 25px;
  75. background-color: #F2F2F2;
  76. justify-content: space-between;
  77. padding: 0 10px;
  78. align-items: center;
  79. font-size: 14px;
  80. }
  81. .navigationBarIcon {
  82. display: flex;
  83. align-items: center;
  84. }
  85. .rotate {
  86. transform: rotate(180deg);
  87. transition: transform .3s;
  88. }
  89. .rotate1 {
  90. transform: rotate(0deg);
  91. transition: transform .3s;
  92. }
  93. .content {
  94. border-bottom: 1px solid #DCDFE6;
  95. }
  96. </style>

发表评论

表情:
评论列表 (有 0 条评论,542人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jQuery 折叠面板

    手风琴是非常常见而实用的效果,本篇文章示例一个简单的手风琴效果。 上效果图: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_