vue自定義實現(xiàn)計算器組件(附完整代碼)
更新時間:2025年06月09日 11:23:10 作者:S筱瀟S四維Smile
這篇文章主要為大家詳細介紹了如何使用Vue自定義實現(xiàn)一個計算器組件,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
自定義組件實現(xiàn)以下簡單的計算器功能:

創(chuàng)建計算器組件文件calculator.vue,代碼如下:
<template>
<div class="calculator">
<!-- 當前運算過程顯示區(qū)域 -->
<div class="expression">{{ currentExpression }}</div>
<!-- 計算器顯示屏 -->
<div class="display">{{ displayValue }}</div>
<!-- 按鈕區(qū)域 -->
<div class="buttons">
<el-button @click="clear">AC</el-button>
<el-button @click="toggleSign">+/-</el-button>
<el-button @click="inputPercent">%</el-button>
<el-button @click="inputOperator('÷')" style="font-size: 22px;">÷</el-button>
<el-button @click="inputDigit(7)">7</el-button>
<el-button @click="inputDigit(8)">8</el-button>
<el-button @click="inputDigit(9)">9</el-button>
<el-button @click="inputOperator('*')">*</el-button>
<el-button @click="inputDigit(4)">4</el-button>
<el-button @click="inputDigit(5)">5</el-button>
<el-button @click="inputDigit(6)">6</el-button>
<el-button @click="inputOperator('-')">-</el-button>
<el-button @click="inputDigit(1)">1</el-button>
<el-button @click="inputDigit(2)">2</el-button>
<el-button @click="inputDigit(3)">3</el-button>
<el-button @click="inputOperator('+')">+</el-button>
<el-button @click="inputDigit(0)" class="zero">0</el-button>
<el-button @click="inputDot">.</el-button>
<el-button @click="calculate">=</el-button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
displayValue: '0', // 顯示的值
firstOperand: null, // 第一個操作數(shù)
waitingForSecondOperand: false, // 是否等待第二個操作數(shù)
operator: null, // 當前的操作符
currentExpression: '' // 用于存儲當前運算過程
};
},
methods: {
// 輸入數(shù)字
inputDigit(digit) {
// 如果運算結束并開始輸入新數(shù)字,清空計算過程和顯示屏
if (this.firstOperand !== null && this.operator === null) {
this.displayValue = String(digit);
this.currentExpression = ''; // 清空當前運算過程
this.firstOperand = null; // 重置操作數(shù)
} else if (this.waitingForSecondOperand) {
this.displayValue = String(digit);
this.waitingForSecondOperand = false;
} else {
this.displayValue = this.displayValue === '0' ? String(digit) : this.displayValue + String(digit);
}
this.updateExpression();
},
// 輸入小數(shù)點
inputDot() {
if (!this.displayValue.includes('.')) {
this.displayValue += '.';
}
this.updateExpression();
},
// 處理運算符
inputOperator(nextOperator) {
const inputValue = parseFloat(this.displayValue);
if (this.operator && this.waitingForSecondOperand) {
this.operator = nextOperator;
this.updateExpression();
return;
}
if (this.firstOperand === null) {
this.firstOperand = inputValue;
} else if (this.operator) {
const result = this.performCalculation(this.firstOperand, inputValue, this.operator);
this.displayValue = String(result);
this.firstOperand = result;
}
this.waitingForSecondOperand = true;
this.operator = nextOperator;
this.updateExpression();
},
// 執(zhí)行計算
performCalculation(firstOperand, secondOperand, operator) {
switch (operator) {
case '+':
return firstOperand + secondOperand;
case '-':
return firstOperand - secondOperand;
case '*':
return firstOperand * secondOperand;
case '÷':
return firstOperand / secondOperand;
default:
return secondOperand;
}
},
// 計算結果
calculate() {
// 如果未輸入第二個操作數(shù),直接返回,不執(zhí)行計算
if (this.operator && this.waitingForSecondOperand) {
return;
}
if (this.operator) {
const inputValue = parseFloat(this.displayValue);
const secondOperand = this.waitingForSecondOperand ? this.firstOperand : inputValue; // 如果沒有第二個操作數(shù),則使用第一個操作數(shù)
const result = this.performCalculation(this.firstOperand, secondOperand, this.operator);
// 完整地記錄本次運算過程
this.currentExpression = `${this.firstOperand} ${this.operator} ${secondOperand} = ${result}`;
this.displayValue = String(result);
this.firstOperand = result;
this.operator = null;
this.waitingForSecondOperand = false;
}
},
// 清除
clear() {
this.displayValue = '0';
this.firstOperand = null;
this.operator = null;
this.waitingForSecondOperand = false;
this.currentExpression = ''; // 清空當前運算過程
},
// 刪除最后一個輸入的字符
deleteLast() {
this.displayValue = this.displayValue.length > 1 ? this.displayValue.slice(0, -1) : '0';
this.updateExpression();
},
// 改變符號
toggleSign() {
this.displayValue = String(parseFloat(this.displayValue) * -1);
this.updateExpression();
},
// 處理百分比
inputPercent() {
this.displayValue = String(parseFloat(this.displayValue) / 100);
this.updateExpression();
},
// 更新當前運算過程的顯示內(nèi)容
updateExpression() {
if (this.firstOperand !== null && this.operator) {
// 如果還未輸入第二個操作數(shù),不顯示 displayValue
this.currentExpression = this.waitingForSecondOperand ?
`${this.firstOperand} ${this.operator}` :
`${this.firstOperand} ${this.operator} ${this.displayValue}`;
} else {
this.currentExpression = this.displayValue;
}
console.log('this.currentExpression', this.currentExpression)
}
}
};
</script>
<style scoped>
.calculator {
width: 400px;
margin: 0 auto;
padding: 20px;
background-color: #f1f1f1;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.expression {
min-height: 30px;
color: #888;
text-align: right;
font-size: 16px;
margin-bottom: 5px;
}
.display {
width: 100%;
min-height: 60px;
background-color: #333;
color: white;
text-align: right;
padding: 10px;
font-size: 24px;
border-radius: 5px;
margin-bottom: 10px;
word-wrap: break-word;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
width: 100%;
font-size: 18px;
border-radius: 5px;
background-color: #fff;
border: 1px solid #ccc;
}
.zero {
grid-column: span 2;
}
>>>.el-button+.el-button {
margin-left: 0;
}
</style>在項目中引用:
import Calculator from '@/components/common/calculator'
export default {
components:{
Calculator
},
}
使用標簽即可:
<Calculator></Calculator>
以上就是vue自定義實現(xiàn)計算器組件(附完整代碼)的詳細內(nèi)容,更多關于Vue計算器的資料請關注腳本之家其它相關文章!
相關文章
Vue3+El-Plus實現(xiàn)表格行拖拽功能完整代碼
在vue3+elementPlus網(wǎng)站開發(fā)中,詳細完成el-table表格的鼠標拖拽/拖曳/拖動排序,下面這篇文章主要給大家介紹了關于Vue3+El-Plus實現(xiàn)表格行拖拽功能的相關資料,需要的朋友可以參考下2024-05-05
vue部署到線上為啥會出現(xiàn)404的原因分析及解決
這篇文章主要介紹了vue部署到線上為啥會出現(xiàn)404的原因分析及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

