当前位置:首页 > VUE

vue实现计算机

2026-02-24 19:14:28VUE

Vue 实现计算器的方法

使用 Vue 实现计算器可以结合其响应式数据绑定和组件化特性,以下是一个完整的实现方案:

创建 Vue 项目

通过 Vue CLI 或 Vite 初始化项目,确保安装最新版本的 Vue 3:

vue实现计算机

npm init vue@latest calculator
cd calculator
npm install

核心组件结构

src/App.vue 中构建计算器主体:

vue实现计算机

<template>
  <div class="calculator">
    <div class="display">{{ currentDisplay }}</div>
    <div class="buttons">
      <button 
        v-for="btn in buttons" 
        :key="btn" 
        @click="handleClick(btn)"
        :class="{ 'operator': ['+','-','×','÷'].includes(btn) }"
      >
        {{ btn }}
      </button>
    </div>
  </div>
</template>

数据与逻辑实现

<script setup>
import { ref } from 'vue';

const currentDisplay = ref('0');
const firstOperand = ref(null);
const operator = ref(null);
const waitingForSecondOperand = ref(false);

const buttons = [
  '7', '8', '9', '÷',
  '4', '5', '6', '×',
  '1', '2', '3', '-',
  '0', 'C', '=', '+'
];

const handleClick = (button) => {
  if (!isNaN(button) || button === '.') {
    handleNumberInput(button);
  } else if (['+','-','×','÷'].includes(button)) {
    handleOperator(button);
  } else if (button === '=') {
    handleEquals();
  } else if (button === 'C') {
    handleClear();
  }
};

const handleNumberInput = (num) => {
  if (waitingForSecondOperand.value) {
    currentDisplay.value = num;
    waitingForSecondOperand.value = false;
  } else {
    currentDisplay.value = currentDisplay.value === '0' ? num : currentDisplay.value + num;
  }
};

const handleOperator = (op) => {
  const inputValue = parseFloat(currentDisplay.value);

  if (firstOperand.value === null) {
    firstOperand.value = inputValue;
  } else if (operator.value) {
    const result = calculate(firstOperand.value, parseFloat(currentDisplay.value), operator.value);
    currentDisplay.value = String(result);
    firstOperand.value = result;
  }

  operator.value = op;
  waitingForSecondOperand.value = true;
};

const handleEquals = () => {
  if (operator.value && !waitingForSecondOperand.value) {
    const result = calculate(firstOperand.value, parseFloat(currentDisplay.value), operator.value);
    currentDisplay.value = String(result);
    firstOperand.value = null;
    operator.value = null;
  }
};

const handleClear = () => {
  currentDisplay.value = '0';
  firstOperand.value = null;
  operator.value = null;
  waitingForSecondOperand.value = false;
};

const calculate = (first, second, op) => {
  switch (op) {
    case '+': return first + second;
    case '-': return first - second;
    case '×': return first * second;
    case '÷': return first / second;
    default: return second;
  }
};
</script>

样式设计

<style scoped>
.calculator {
  width: 300px;
  margin: 50px auto;
  border: 1px solid #ccc;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 20px rgba(0,0,0,0.1);
}

.display {
  background: #222;
  color: white;
  font-size: 2em;
  padding: 20px;
  text-align: right;
}

.buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: #ddd;
}

button {
  border: none;
  padding: 20px;
  font-size: 1.2em;
  cursor: pointer;
  background: white;
  transition: background 0.3s;
}

button:hover {
  background: #f0f0f0;
}

button.operator {
  background: #f5923e;
  color: white;
}

button.operator:hover {
  background: #e07d2e;
}
</style>

功能扩展建议

  • 添加键盘事件支持:监听键盘输入实现物理键盘操作
    
    onMounted(() => {
    window.addEventListener('keydown', handleKeyDown);
    });

onUnmounted(() => { window.removeEventListener('keydown', handleKeyDown); });

const handleKeyDown = (e) => { const key = e.key; if (/[0-9.]/.test(key)) handleClick(key); else if (key === '+') handleClick('+'); // 其他按键处理... };



- 历史记录功能:使用 Vuex 或 Pinia 存储计算历史
- 科学计算功能:扩展运算符处理更复杂的数学运算
- 主题切换:通过 CSS 变量实现暗黑模式等主题切换

该实现完整覆盖了基本计算器的所有功能,包括四则运算、连续运算、清除功能等,响应式设计确保在不同设备上正常显示。

标签: 计算机vue
分享给朋友:

相关文章

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Des…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FF…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…