首页 > 解决方案 > 我在 vue 中做了一个计算器,我希望它能够输入最后一个运算符并更改前一个(js 有问题)

问题描述

我有这个数据

  value: '',
  numbers: ['C','*','/','-',7,8,9,'+',4,5,6,'%',1,2,3,'=',0,'.'],
  operator: '',
  newvalue: '',

实际上,当您输入“8+9”时它正在工作,但我希望我的计算器在您输入“8+-”时更改运算符,它必须删除 + 并将其替换为 - 但我的代码的工作方式类似于“8+-9” " 用这个 if 语句

    if (['/','*','-','+','%'].includes(x)) {
    this.operator = x;
    if (this.value == this.operator) {
      this.value = this.operator
    }else {
      this.value += this.operator;
    }
  }

  if (x === '=') {
    this.value = eval(
      this.newvalue + this.operator + this.value
    );
    this.newvalue = '';
    this.operator = '';
  }
}

这是我在 github 上的完整代码,如果有人能提供帮助,我将不胜感激 https://github.com/rozhansh43/vue-calculator/blob/master/src/components/Calculator.vue

标签: javascriptvue.js

解决方案


推荐阅读