1. 设计组件

    分析组件的特性,使得组件得到最大的复用(通用性)
    20220302142539842.png

    1. 编写HTML和样式

      1. <template>
      2. <div class="m-card">
      3. <div class="m-card-img">
      4. <img alt="img" />
      5. </div>
      6. <div class="m-card-summary">summary</div>
      7. <div class="footer"></div>
      8. </div>
      9. </template>
      10. <script>
      11. export default {
      12. name: "m-card",
      13. props: {
      14. width: { //卡片的宽度
      15. type: Number,
      16. default: 0
      17. },
      18. imgSrc: { //卡片图片的资源地址
      19. type: String,
      20. default: "",
      21. require: true
      22. },
      23. imgHeight: {//卡片图片的高度
      24. type: Number,
      25. default: 0
      26. },
      27. summary: {//卡片的概要
      28. type: String,
      29. default: ""
      30. }
      31. }
      32. }
      33. </script>

      ```css .m-card { width: 270px; border-radius: 8px; background-color: #fff; overflow: hidden; box-shadow: 0 6px 10px 0 rgba(95, 101, 105, 0.15); padding-bottom: 8px; //&-img —-> m-card-img &-img { height: 152px; img { width: 100%; height: 100%; } } &-summary { padding: 8px; text-align: left; font-size: 14px; } }

    1. 3. 编写逻辑代码
    2. ```vue
    3. <template>
    4. <div class="m-card" :style="width ? { width: width + 'px' } : {}">
    5. <div class="m-card-img" :style="imgHeight ? { height: imgHeight + 'px' } : {}">
    6. <img :src="imgSrc" alt="img" />
    7. </div>
    8. <div class="m-card-summary" v-if="summary">{{ summary }}</div>
    9. <div class="m-card-summary" v-else>
    10. <slot></slot>
    11. </div>
    12. <slot name="footer"></slot>
    13. </div>
    14. </template>
    15. <script>
    16. export default {
    17. name: "m-card",
    18. props: {
    19. width: { //卡片的宽度
    20. type: Number,
    21. default: 0
    22. },
    23. imgSrc: { //卡片图片的资源地址
    24. type: String,
    25. default: "",
    26. require: true
    27. },
    28. imgHeight: {//卡片图片的高度
    29. type: Number,
    30. default: 0
    31. },
    32. summary: {//卡片的概要
    33. type: String,
    34. default: ""
    35. }
    36. }
    37. }
    38. </script>
    1. 测试组件 ```vue

    ```