Vue实现简单的计算器

首先呢, 看了一下没找到带数字键盘的demo,于是乎自己写了个记录一下。

Vue+antd 组件 原理也超级简单,利用eval 计算 大佬轻点喷

<template>
  <div id="app" style="width: 800px">
    <a-row>
      <a-col>
        <a-input type="text" value="" v-model="rel"></a-input>
        <a-input type="button" value="del" @click="del()"> DEL</a-input>
        <a-input type="button" value="C" @click="clean()">C</a-input>
      </a-col>
    </a-row>
    <a-row>
      <a-col :span="6">
        <a-input type="button" value="7" @click="add(7)">7</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="8" @click="add(8)">8</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="9" @click="add(9)">9</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="+" @click="add('+')">+</a-input>
      </a-col>
    </a-row>
    <a-row>
      <a-col :span="6">
        <a-input type="button" value="4" @click="add(4)">4</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="5" @click="add(5)">5</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="6" @click="add(6)">6</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="-" @click="add('-')">-</a-input>
      </a-col>
    </a-row>
    <a-row>
      <a-col :span="6">
        <a-input type="button" value="1" @click="add(1)">1</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="2" @click="add(2)">2</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="3" @click="add(3)">3</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="*" @click="add('*')">*</a-input>
      </a-col>
    </a-row>
    <a-row>
      <a-col :span="6">
        <a-input type="button" value="0" @click="add(0)">0</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="." @click="add('.')">.</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="/" @click="add('/')">/</a-input>
      </a-col>
      <a-col :span="6">
        <a-input type="button" value="=" @click="result('=')">=</a-input>
      </a-col>
    </a-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rel: "",
    };
  },
  methods: {
    add(index) {
      this.rel += index;
    },
    del() {
      this.rel = this.rel.substring(0, this.rel.length - 1);
    },
    result() {
      this.rel = String(this.rel);
      this.rel = eval(this.rel);
    },
    clean() {
      this.rel = "";
    },
  },
};
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值