• Vue3自定义文章列表组件


    一、Vue3的代码展示

    <template>
      <div>
        <div v-for="article in articles" :key="article.id" class="article-card">
          <div class="author-info">
            <img :src="article.avatar" alt="Author Avatar" class="avatar" />
            <div class="author-details">
              <span class="author-name">{{ article.username }}span>
              <span class="publish-time">{{ article.createTime }}span>
            div>
          div>
          <div class="article-details">
            <h3 class="article-title">{{ article.title }}h3>
            <p class="article-info">{{ article.summary }}p>
          div>
          <div class="article-stats">
            <span class="stat-item">查看数: {{ article.viewCount }}span>
            <span class="stat-item">点赞数: {{ article.likeCount }}span>
            <span class="stat-item">评论数: {{ article.commentCount }}span>
          div>
        div>
      div>
    template>
    <script setup>
    const props = defineProps(['articles']);
    script>
    <style scoped>
    .article-card {
      border: 1px solid #ddd;
      padding: 16px;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      transition: background-color 0.3s; /* 添加过渡以实现平滑的颜色变化 */
    }
    
    .article-card:hover {
      background-color: #fafafa; /* 在悬停时改变背景颜色 */
    }
    
    .author-info {
      display: flex;
      align-items: center;
      margin-bottom: 8px;
    }
    
    .avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      margin-right: 8px;
    }
    
    .author-details {
      display: flex;
      align-items: baseline;
    }
    
    .author-name {
      font-weight: bold;
      margin-right: 4px;
    }
    
    .publish-time {
      color: #888;
      margin-left: 20px;
    }
    
    .article-details {
      margin-bottom: 12px;
      display: flex;
      flex-wrap: wrap;
      flex-direction: column;
      /*background-color: #3fdbf0;*/
      text-align: left; /* Align content to the left */
    }
    
    .article-title {
      margin-bottom: 2px;
      font-size: 20px;
    }
    
    .article-info {
      color: #555;
      margin-bottom: 8px;
      font-size: 16px;
    }
    
    .article-stats {
      display: flex;
      justify-content: space-between;
      color: #888;
    }
    
    .stat-item {
      margin-right: 12px; 
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    二、 代码解读

    HTML 模板部分: