Vue封装组件-按钮-LjButton

痛定思痛。 2022-09-03 04:28 363阅读 0赞

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70

使用:

  1. <!--menu5-->
  2. <template>
  3. <div class="menu5">
  4. <div>
  5. <LjButton @click="dialogVisible = true">打开弹出框</LjButton>
  6. </div>
  7. <el-dialog
  8. title="提示"
  9. :visible.sync="dialogVisible"
  10. width="30%"
  11. :modal-append-to-body="false"
  12. >
  13. <span>这是一段信息</span>
  14. <span slot="footer" class="dialog-footer">
  15. <LjButton
  16. style="margin: 0 10px"
  17. type="danger"
  18. @click="dialogVisible = false"
  19. >取 消</LjButton
  20. >
  21. <LjButton type="primary" @click="dialogVisible = false">确 定</LjButton>
  22. </span>
  23. </el-dialog>
  24. <div>
  25. <p>
  26. <LjButton>默认按钮</LjButton>
  27. <LjButton type="primary">主要按钮</LjButton>
  28. <LjButton type="success">成功按钮</LjButton>
  29. <LjButton type="info">信息按钮</LjButton>
  30. <LjButton type="warning">警告按钮</LjButton>
  31. <LjButton type="danger">危险按钮</LjButton>
  32. </p>
  33. <p>
  34. <LjButton size="large">large按钮</LjButton>
  35. <LjButton size="medium">medium按钮</LjButton>
  36. <LjButton size="small">small按钮</LjButton>
  37. <LjButton size="mini">mini按钮</LjButton>
  38. </p>
  39. <p>
  40. <LjButton type="default" disabled>默认按钮</LjButton>
  41. <LjButton type="primary" disabled>主要按钮</LjButton>
  42. <LjButton type="success" disabled>成功按钮</LjButton>
  43. <LjButton type="info" disabled>信息按钮</LjButton>
  44. <LjButton type="warning" disabled>警告按钮</LjButton>
  45. <LjButton type="danger" disabled>危险按钮</LjButton>
  46. </p>
  47. <p>
  48. <LjButton round>默认按钮</LjButton>
  49. <LjButton type="primary" round>主要按钮</LjButton>
  50. <LjButton type="success" round>成功按钮</LjButton>
  51. <LjButton type="info" round>信息按钮</LjButton>
  52. <LjButton type="warning" round>警告按钮</LjButton>
  53. <LjButton type="danger" round>危险按钮</LjButton>
  54. </p>
  55. <p>
  56. <LjButton round size="large">默认按钮</LjButton>
  57. <LjButton type="primary" round size="large">主要按钮</LjButton>
  58. <LjButton type="success" round size="large">成功按钮</LjButton>
  59. <LjButton type="info" round size="medium">信息按钮</LjButton>
  60. <LjButton type="warning" round size="small">警告按钮</LjButton>
  61. <LjButton type="danger" round size="mini">危险按钮</LjButton>
  62. </p>
  63. <p>
  64. <LjButton type="text">文字按钮</LjButton>
  65. <LjButton type="text" size="large">文字按钮</LjButton>
  66. <LjButton type="text" size="medium">文字按钮</LjButton>
  67. <LjButton type="text" size="small">文字按钮</LjButton>
  68. <LjButton type="text" size="mini">文字按钮</LjButton>
  69. </p>
  70. <p>
  71. <LjButton type="text" disabled>禁用文字按钮</LjButton>
  72. <LjButton type="text" size="large" disabled>禁用文字按钮</LjButton>
  73. <LjButton type="text" size="medium" disabled>禁用文字按钮</LjButton>
  74. <LjButton type="text" size="small" disabled>禁用文字按钮</LjButton>
  75. <LjButton type="text" size="mini" disabled>禁用文字按钮</LjButton>
  76. </p>
  77. <p>
  78. <LjButton icon="el-icon-upload"></LjButton>
  79. <LjButton icon="el-icon-upload">默认按钮</LjButton>
  80. <LjButton
  81. >默认按钮<i class="el-icon-upload el-icon--right"></i
  82. ></LjButton>
  83. </p>
  84. <p>
  85. <LjButton :loading="true">默认按钮</LjButton>
  86. </p>
  87. <p>
  88. <LjButton circle size="large" icon="el-icon-search"></LjButton>
  89. <LjButton
  90. type="primary"
  91. circle
  92. size="large"
  93. icon="el-icon-edit"
  94. ></LjButton>
  95. <LjButton
  96. type="success"
  97. circle
  98. size="large"
  99. icon="el-icon-check"
  100. ></LjButton>
  101. <LjButton
  102. type="info"
  103. circle
  104. size="medium"
  105. icon="el-icon-message"
  106. ></LjButton>
  107. <LjButton
  108. type="warning"
  109. circle
  110. size="small"
  111. icon="el-icon-star-off"
  112. ></LjButton>
  113. <LjButton
  114. type="danger"
  115. circle
  116. size="mini"
  117. icon="el-icon-delete"
  118. ></LjButton>
  119. </p>
  120. <p>
  121. <LjButton square size="large" icon="el-icon-search"></LjButton>
  122. <LjButton
  123. type="primary"
  124. square
  125. size="large"
  126. icon="el-icon-edit"
  127. ></LjButton>
  128. <LjButton
  129. type="success"
  130. square
  131. size="large"
  132. icon="el-icon-check"
  133. ></LjButton>
  134. <LjButton
  135. type="info"
  136. square
  137. size="medium"
  138. icon="el-icon-message"
  139. ></LjButton>
  140. <LjButton
  141. type="warning"
  142. square
  143. size="small"
  144. icon="el-icon-star-off"
  145. ></LjButton>
  146. <LjButton
  147. type="danger"
  148. square
  149. size="mini"
  150. icon="el-icon-delete"
  151. ></LjButton>
  152. </p>
  153. </div>
  154. </div>
  155. </template>
  156. <script>
  157. import LjButton from "@/components/LjButton/index.vue";
  158. import LjDialog from "@/components/LjDialog/index.vue";
  159. export default {
  160. name: "menu5",
  161. components: { LjButton, LjDialog },
  162. props: {},
  163. data() {
  164. return {
  165. dialogVisible: false,
  166. };
  167. },
  168. computed: {},
  169. created() {},
  170. mounted() {},
  171. filters: {},
  172. methods: {},
  173. watch: {},
  174. };
  175. </script>
  176. <style lang="scss" scoped>
  177. .menu5 {
  178. display: grid;
  179. grid-template-columns: 1fr 1fr;
  180. p {
  181. margin: 40px 0;
  182. display: flex;
  183. gap: 10px;
  184. }
  185. }
  186. </style>

LjButton.vue:

  1. <!--LjButton-->
  2. <template>
  3. <div class="LjButton">
  4. <span
  5. :class="[
  6. typeClass,
  7. sizeClass,
  8. roundClass,
  9. disabledClass,
  10. circleClass,
  11. squareClass,
  12. ]"
  13. @click="clickButton()"
  14. >
  15. <span v-if="loading" style="padding: 2px">
  16. <i class="el-icon-loading"></i>
  17. </span>
  18. <span v-else-if="icon" style="padding: 2px"><i :class="icon" /></span>
  19. <slot></slot>
  20. </span>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: "LjButton",
  26. components: {},
  27. props: {
  28. type: {
  29. type: String,
  30. default: "",
  31. },
  32. size: {
  33. type: String,
  34. default: "",
  35. },
  36. disabled: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. round: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. icon: {
  45. type: String,
  46. default: "",
  47. },
  48. loading: {
  49. type: Boolean,
  50. default: false,
  51. },
  52. circle: {
  53. type: Boolean,
  54. default: false,
  55. },
  56. square: {
  57. type: Boolean,
  58. default: false,
  59. },
  60. },
  61. data() {
  62. return {};
  63. },
  64. computed: {
  65. typeClass() {
  66. return {
  67. "LjButton-default": this.type === "" || this.type === "default",
  68. "LjButton-primary": this.type === "primary",
  69. "LjButton-warning": this.type === "warning",
  70. "LjButton-success": this.type === "success",
  71. "LjButton-info": this.type === "info",
  72. "LjButton-danger": this.type === "danger",
  73. "LjButton-text": this.type === "text",
  74. };
  75. },
  76. sizeClass() {
  77. return {
  78. "LjButton-size-large": this.size === "large",
  79. "LjButton-size-medium": this.size === "" || this.size === "medium",
  80. "LjButton-size-small": this.size === "small",
  81. "LjButton-size-mini": this.size === "mini",
  82. };
  83. },
  84. disabledClass() {
  85. if (this.disabled) {
  86. return { "LjButton-disabled": true };
  87. }
  88. },
  89. roundClass() {
  90. if (this.round) {
  91. return { "LjButton-round": true };
  92. }
  93. },
  94. circleClass() {
  95. if (this.circle) {
  96. return { "LjButton-circle": true };
  97. }
  98. },
  99. squareClass() {
  100. if (this.square) {
  101. return { "LjButton-square": true };
  102. }
  103. },
  104. },
  105. created() {},
  106. mounted() {},
  107. filters: {},
  108. methods: {
  109. clickButton() {
  110. this.$emit("click"); //没有这个的话,组件在使用click点击事件时需要加上native,即@click.native="xxx"
  111. },
  112. },
  113. watch: {},
  114. };
  115. </script>
  116. <style lang="scss" scoped>
  117. $color-default: #1dcbf3;
  118. $color-primary: #1dcbf3;
  119. $color-warning: orange; //#f56c6c
  120. $color-success: #67c23a; //#f56c6c
  121. $color-info: #909399;
  122. $color-danger: red;
  123. $bgColor-primary: rgba(0, 183, 255, 0.3);
  124. $bgColor-warning: rgba(255, 166, 0, 0.3);
  125. $bgColor-success: rgba(103, 194, 58, 0.3); //#f56c6c
  126. $bgColor-info: rgba(144, 147, 153, 0.3); //#f56c6c
  127. $bgColor-danger: rgba(255, 0, 0, 0.3); //#f56c6c
  128. .LjButton {
  129. display: inline-block;
  130. caret-color: transparent;
  131. .LjButton-default {
  132. border: 1px solid white;
  133. color: white;
  134. &:hover {
  135. cursor: pointer;
  136. color: $color-default;
  137. border: 1px solid $color-default;
  138. }
  139. }
  140. .LjButton-primary {
  141. border: 1px solid $color-primary;
  142. color: $color-primary;
  143. &:hover {
  144. cursor: pointer;
  145. background-color: $bgColor-primary;
  146. }
  147. }
  148. .LjButton-warning {
  149. border: 1px solid $color-warning;
  150. color: $color-warning;
  151. &:hover {
  152. cursor: pointer;
  153. background-color: $bgColor-warning;
  154. }
  155. }
  156. .LjButton-success {
  157. border: 1px solid $color-success;
  158. color: $color-success;
  159. &:hover {
  160. cursor: pointer;
  161. background-color: $bgColor-success;
  162. }
  163. }
  164. .LjButton-info {
  165. border: 1px solid $color-info;
  166. color: $color-info;
  167. &:hover {
  168. cursor: pointer;
  169. background-color: $bgColor-info;
  170. }
  171. }
  172. .LjButton-danger {
  173. border: 1px solid $color-danger;
  174. color: $color-danger;
  175. &:hover {
  176. cursor: pointer;
  177. background-color: $bgColor-danger;
  178. }
  179. }
  180. .LjButton-text {
  181. color: white;
  182. &:hover {
  183. cursor: pointer;
  184. color: $color-default;
  185. }
  186. }
  187. //size
  188. .LjButton-size-large {
  189. font-size: 20px;
  190. padding: 11px 16px;
  191. }
  192. .LjButton-size-medium {
  193. font-size: 18px;
  194. padding: 9px 13px;
  195. }
  196. .LjButton-size-small {
  197. font-size: 16px;
  198. padding: 6px 9px;
  199. }
  200. .LjButton-size-mini {
  201. font-size: 14px;
  202. padding: 4px 6px;
  203. }
  204. //disabled
  205. .LjButton-disabled {
  206. color: gray;
  207. &:hover {
  208. cursor: not-allowed;
  209. }
  210. }
  211. //round
  212. .LjButton-round {
  213. border-radius: 10px;
  214. }
  215. //circle
  216. .LjButton-circle {
  217. border-radius: 50%;
  218. padding: 7px;
  219. }
  220. //square
  221. .LjButton-square {
  222. border-radius: 20%;
  223. padding: 7px;
  224. }
  225. }
  226. </style>

发表评论

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

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

相关阅读

    相关 Vue封装组件的过程

    vue组件的定义 ● 组件(Component)是Vue.js最强大的功能之一 ● 组件可以扩展HTML元素,封装可重用代码 ● 在较高层面上,组件是自定义元素,Vue.

    相关 Vue封装树形菜单组件

    csdn终于更新完成了哈,ok,步入正题 像这种树形结构的核心思想就是:递归思想,知道使用递归,其实这个例子函数的封装也就很简单喽  实现步骤: