• 组件封装 - 按钮组件和计数组件


    这两个组件还是很简单的

    按钮组件, 主要是靠的 css 来改变不同的大小, 颜色

    数组件, 它和确认框组件的逻辑如出一辙; 都是使用 v-model 去驱动

    先来看看效果

    先完成, 按钮组件

     思路分析:

    1. 基本布局就是一个 button 按钮, 和一个默认插槽

    2. 通过父组件传入的 type 和 size 属性去动态的给 button 标签添加 class 类

    3. 准备好所有情况的样式

    1. <template>
    2. <button class="xtx-button ellipsis" :class="[size,type]">
    3. <slot />
    4. button>
    5. template>
    6. <script>
    7. export default {
    8. name: 'XtxButton',
    9. props: {
    10. size: {
    11. type: String,
    12. default: 'middle'
    13. },
    14. type: {
    15. type: String,
    16. default: 'default'
    17. }
    18. }
    19. }
    20. script>
    21. <style scoped lang="less">
    22. .xtx-button {
    23. appearance: none;
    24. border: none;
    25. outline: none;
    26. background: #fff;
    27. text-align: center;
    28. border: 1px solid transparent;
    29. border-radius: 4px;
    30. cursor: pointer;
    31. margin-top: 20px;
    32. }
    33. .large {
    34. width: 240px;
    35. height: 50px;
    36. font-size: 16px;
    37. }
    38. .middle {
    39. width: 180px;
    40. height: 50px;
    41. font-size: 16px;
    42. }
    43. .small {
    44. width: 100px;
    45. height: 32px;
    46. font-size: 14px;
    47. }
    48. .mini {
    49. width: 60px;
    50. height: 32px;
    51. font-size: 14px;
    52. }
    53. .default {
    54. border-color: #e4e4e4;
    55. color: #666;
    56. }
    57. .primary {
    58. border-color: @xtxColor;
    59. background: @xtxColor;
    60. color: #fff;
    61. }
    62. .plain {
    63. border-color: @xtxColor;
    64. color: @xtxColor;
    65. background: lighten(@xtxColor,50%);
    66. }
    67. .gray {
    68. border-color: #ccc;
    69. background: #ccc;;
    70. color: #fff;
    71. }
    72. style>

    计数组件

    思路分析:

    1. 父组件定义 v-model 所依赖的数据

    2. 技术组件使用 props 接收 modelValue 数据

    3. 使用 @vue/usecore 中的 useVmodel 方法去接收 modelValue 的数据

    4. 用户点击计数时, 将修改的数据传给父组件

    5. emit 一个 change 事件, 方便父组件可以拿到修改的值

    1. <template>
    2. <div class="xtx-numbox">
    3. <div class="label">{{ label }}div>
    4. <div class="numbox">
    5. <a href="javascript:;" @click="changeNumber(-1)">-a>
    6. <input type="text" readonly :value=" num">
    7. <a href="javascript:;" @click="changeNumber(1)">+a>
    8. div>
    9. div>
    10. template>
    11. <script>
    12. import { useVModel } from '@vueuse/core'
    13. export default {
    14. name: 'XtxNumbox',
    15. props: {
    16. modelValue: {
    17. type: Number,
    18. default: 1
    19. },
    20. label: {
    21. type: String,
    22. default: '数量'
    23. },
    24. max: {
    25. type: Number,
    26. default: 100
    27. },
    28. min: {
    29. type: Number,
    30. default: 1
    31. }
    32. },
    33. setup (props, { emit }) {
    34. const num = useVModel(props, 'modelValue', emit)
    35. const changeNumber = (step) => {
    36. const newNum = num.value + step
    37. if (newNum > props.max || newNum < props.min) return
    38. num.value = newNum
    39. emit('change', newNum)
    40. }
    41. return { changeNumber, num }
    42. }
    43. }
    44. script>
    45. <style scoped lang="less">
    46. .xtx-numbox {
    47. display: flex;
    48. align-items: center;
    49. .label {
    50. width: 60px;
    51. color: #999;
    52. padding-left: 10px;
    53. }
    54. .numbox {
    55. width: 120px;
    56. height: 30px;
    57. border: 1px solid #e4e4e4;
    58. display: flex;
    59. > a {
    60. width: 29px;
    61. line-height: 28px;
    62. text-align: center;
    63. background: #f8f8f8;
    64. font-size: 16px;
    65. color: #666;
    66. &:first-of-type {
    67. border-right:1px solid #e4e4e4;
    68. }
    69. &:last-of-type {
    70. border-left:1px solid #e4e4e4;
    71. }
    72. }
    73. > input {
    74. width: 60px;
    75. padding: 0 5px;
    76. text-align: center;
    77. color: #666;
    78. }
    79. }
    80. }
    81. style>

  • 相关阅读:
    【刷题专栏—突破思维】142. 环形链表 II
    3D~RPG游戏的制作
    【计组笔记】06_指令系统
    MindSpore-AttributeError: module ‘mindspore‘ has no attribute ‘value_and_grad‘
    【回归预测-BP预测】基于灰狼算法优化BP神经网络实现数据预测(多输入多输出)含Matlab代码
    SR800-D 5G工业路由器:将无人驾驶汽车的通信能力提升到极限
    测试用例设计方法六脉神剑——第二剑:招式组合,因果判定出世
    详解nvim内建LSP体系与基于nvim-cmp的代码补全体系
    健身房管理系统
    和我一起写一个音乐播放器,听一首最伟大的作品
  • 原文地址:https://blog.csdn.net/weixin_48524561/article/details/127443656