当前位置:首页 > VUE

vue实现成绩显示

2026-02-23 06:34:35VUE

Vue 实现成绩显示功能

数据准备与绑定

在 Vue 中,成绩数据通常以数组或对象形式存储在组件的 data 属性中。例如:

data() {
  return {
    scores: [
      { name: '数学', score: 90 },
      { name: '语文', score: 85 },
      { name: '英语', score: 92 }
    ]
  }
}

模板渲染

使用 v-for 指令循环渲染成绩列表,并通过 v-bind: 动态绑定样式或类名:

<ul>
  <li v-for="(item, index) in scores" :key="index">
    {{ item.name }}: {{ item.score }}
  </li>
</ul>

条件样式

根据成绩高低显示不同颜色(如优秀为绿色,及格为黄色):

<li 
  v-for="(item, index) in scores" 
  :key="index"
  :style="{ color: item.score >= 90 ? 'green' : item.score >= 60 ? 'orange' : 'red' }"
>
  {{ item.name }}: {{ item.score }}
</li>

计算属性

通过计算属性处理复杂逻辑,例如计算平均分:

computed: {
  averageScore() {
    const sum = this.scores.reduce((total, item) => total + item.score, 0);
    return (sum / this.scores.length).toFixed(2);
  }
}

组件化

将成绩列表封装为可复用的子组件:

// ScoreItem.vue
<template>
  <div :class="['score-item', scoreClass]">
    <span>{{ name }}</span>
    <span>{{ score }}</span>
  </div>
</template>

<script>
export default {
  props: ['name', 'score'],
  computed: {
    scoreClass() {
      return this.score >= 90 ? 'excellent' : this.score >= 60 ? 'pass' : 'fail';
    }
  }
}
</script>

动态更新

通过方法实现成绩的动态添加或修改:

methods: {
  addScore() {
    this.scores.push({ name: '新科目', score: 0 });
  },
  updateScore(index, newScore) {
    this.$set(this.scores, index, { ...this.scores[index], score: newScore });
  }
}

完整示例

结合以上内容实现的完整组件:

vue实现成绩显示

<template>
  <div>
    <h3>成绩单</h3>
    <ScoreItem 
      v-for="(item, index) in scores" 
      :key="index"
      :name="item.name"
      :score="item.score"
    />
    <p>平均分: {{ averageScore }}</p>
    <button @click="addScore">添加科目</button>
  </div>
</template>

<script>
import ScoreItem from './ScoreItem.vue';

export default {
  components: { ScoreItem },
  data() {
    return {
      scores: [
        { name: '数学', score: 90 },
        { name: '语文', score: 85 },
        { name: '英语', score: 92 }
      ]
    };
  },
  computed: {
    averageScore() {
      const sum = this.scores.reduce((total, item) => total + item.score, 0);
      return (sum / this.scores.length).toFixed(2);
    }
  },
  methods: {
    addScore() {
      this.scores.push({ name: '新科目', score: 0 });
    }
  }
};
</script>

标签: 成绩vue
分享给朋友:

相关文章

vue 实现目录

vue 实现目录

Vue 实现目录的方法 在 Vue 项目中实现目录功能,可以通过以下几种方式实现,具体取决于需求和技术栈。 使用 Vue Router 实现路由目录 Vue Router 是 Vue.js 的官方路…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…