
Product
Introducing Manifest Alerts
Socket now detects supply chain risks in project manifests, starting with missing lockfiles that can make dependency installs non-reproducible.
vue3-calculator
Advanced tools
一个功能完整的 Vue 3 计算器组件,支持标准和科学计算模式。
npm install vue3-calculator
# 或
yarn add vue3-calculator
# 或
pnpm add vue3-calculator
<template>
<div>
<Calculator
:mode="calculatorMode"
@mode-change="handleModeChange"
@calculate="handleCalculate"
@error="handleError"
/>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { Calculator } from "vue3-calculator";
import "vue3-calculator/dist/style.css";
const calculatorMode = ref<"standard" | "scientific">("standard");
function handleModeChange(mode: "standard" | "scientific") {
calculatorMode.value = mode;
console.log("计算器模式切换到:", mode);
}
function handleCalculate(result: string) {
console.log("计算结果:", result);
}
function handleError(error: string) {
console.error("计算错误:", error);
}
</script>
import { createApp } from "vue";
import { install as CalculatorPlugin } from "vue3-calculator";
import "vue3-calculator/dist/style.css";
import App from "./App.vue";
const app = createApp(App);
app.use(CalculatorPlugin);
app.mount("#app");
如果你需要在其他组件中访问计算器的状态,可以直接使用 hook:
<template>
<div>
<p>当前显示: {{ displayText }}</p>
<p>计算表达式: {{ expressionText }}</p>
<p>内存值: {{ memoryValue }}</p>
<p>历史记录数量: {{ history.length }}</p>
<button @click="clearCalculator">清除</button>
<button @click="addToMemory">M+</button>
<button @click="recallMemory">MR</button>
</div>
</template>
<script setup lang="ts">
import { useCalculator } from "vue3-calculator";
const {
displayText,
expressionText,
memoryValue,
history,
clearAll,
memoryAdd,
memoryRecall
} = useCalculator();
function clearCalculator() {
clearAll();
}
function addToMemory() {
memoryAdd();
}
function recallMemory() {
memoryRecall();
}
</script>
<template>
<div class="calculator-container">
<!-- 完整功能的科学计算器 -->
<Calculator
mode="scientific"
:show-toolbar="true"
:show-memory="true"
:show-history="true"
@mode-change="onModeChange"
@calculate="onCalculate"
@error="onError"
@memory-change="onMemoryChange"
/>
<!-- 简化版标准计算器 -->
<Calculator
mode="standard"
:show-toolbar="false"
:show-memory="false"
:show-history="false"
class="simple-calculator"
/>
</div>
</template>
<script setup lang="ts">
import { Calculator } from "vue3-calculator";
import "vue3-calculator/dist/style.css";
function onModeChange(mode: 'standard' | 'scientific') {
console.log('模式切换:', mode);
}
function onCalculate(result: string) {
console.log('计算结果:', result);
// 可以在这里处理计算结果,比如发送到服务器
}
function onError(error: string) {
console.error('计算错误:', error);
// 可以在这里显示错误提示
}
function onMemoryChange(memoryValue: string) {
console.log('内存值变化:', memoryValue);
}
</script>
<style>
.calculator-container {
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.simple-calculator {
max-width: 300px;
}
</style>
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
mode | 'standard' | 'scientific' | 'standard' | 计算器模式 |
showToolbar | boolean | true | 是否显示工具栏 |
showMemory | boolean | true | 是否显示内存功能 |
showHistory | boolean | true | 是否显示历史记录 |
| 事件名 | 参数 | 说明 |
|---|---|---|
mode-change | mode: 'standard' | 'scientific' | 计算器模式改变时触发 |
calculate | result: string | 计算完成时触发 |
error | error: string | 计算出错时触发 |
memory-change | memoryValue: string | 内存值变化时触发 |
history-add | historyItem: HistoryItem | 添加历史记录时触发 |
clear | - | 清除操作时触发 |
input | value: string | 数字或操作符输入时触发 |
0-9: 数字输入+, -, *, /: 基本运算符Enter 或 =: 等于Escape: 清除所有Backspace: 退格.: 小数点%: 百分号组件使用 CSS 变量,你可以通过覆盖这些变量来自定义样式:
.calculator {
/* 字体大小 */
--font-size-small: 11px;
--font-size-normal: 14px;
--font-size-large: 18px;
--font-size-display: 24px;
/* 颜色主题 */
--primary-color: #007acc;
--secondary-color: #f0f0f0;
--background-color: #ffffff;
--text-color: #333333;
--border-color: #e0e0e0;
/* 按钮样式 */
--button-background: #f8f9fa;
--button-hover: #e9ecef;
--button-active: #dee2e6;
--button-operator: #007acc;
--button-operator-hover: #0056b3;
/* 间距 */
--padding-small: 8px;
--padding-medium: 12px;
--padding-large: 16px;
--border-radius: 4px;
}
.calculator.dark-theme {
--background-color: #2d3748;
--text-color: #ffffff;
--border-color: #4a5568;
--button-background: #4a5568;
--button-hover: #718096;
--button-active: #a0aec0;
--primary-color: #63b3ed;
--button-operator: #63b3ed;
--button-operator-hover: #3182ce;
}
.calculator .btn {
transition: all 0.2s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.calculator .btn:hover {
transform: translateY(-1px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}
.calculator .btn-operator {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
}
A: 本组件具有以下优势:
A: 组件内部使用 Decimal.js 处理所有数值计算,支持任意精度的大数运算,不会出现科学计数法显示问题。
A: 支持完整的键盘操作,包括数字键、运算符、回车计算、ESC清除等,详见键盘支持部分。
A: 组件提供了丰富的 CSS 变量,可以轻松自定义颜色、字体、间距等样式,也支持深色主题。
A: 打包后的组件大小约为 64KB (UMD) / 89KB (ES),包含了所有数学计算库,体积合理。
interface CalculatorState {
displayText: Ref<string>; // 当前显示的数值
expressionText: Ref<string>; // 当前计算表达式
memoryValue: Ref<string>; // 内存中的值
history: Ref<HistoryItem[]>; // 计算历史记录
mode: Ref<'standard' | 'scientific'>; // 计算器模式
}
interface CalculatorActions {
// 基础操作
inputNumber: (num: string) => void;
inputOperator: (op: string) => void;
calculate: () => void;
clearAll: () => void;
clearEntry: () => void;
backspace: () => void;
// 内存操作
memoryClear: () => void;
memoryRecall: () => void;
memoryAdd: () => void;
memorySubtract: () => void;
memoryStore: () => void;
// 科学计算
calculateFunction: (func: string) => void;
// 模式切换
toggleMode: () => void;
}
interface HistoryItem {
id: string;
expression: string;
result: string;
timestamp: number;
}
type CalculatorMode = 'standard' | 'scientific';
type OperatorType = '+' | '-' | '*' | '/' | '%' | '^' | '√' | 'sin' | 'cos' | 'tan' | 'ln' | 'log';
# 克隆项目
git clone <repository-url>
cd vue3-calculator
# 安装依赖
npm install
# 开发模式
npm run dev
# 构建
npm run build
# 类型检查
npm run type-check
MIT License
欢迎提交 Issue 和 Pull Request!
FAQs
A Vue 3 calculator component with scientific and standard modes
The npm package vue3-calculator receives a total of 5 weekly downloads. As such, vue3-calculator popularity was classified as not popular.
We found that vue3-calculator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now detects supply chain risks in project manifests, starting with missing lockfiles that can make dependency installs non-reproducible.

Research
/Security News
The trojanized extensions use TinyGo-compiled WebAssembly and Solana transaction memos to resolve command-and-control infrastructure.

Security News
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.