computed简单实现一个计算器

117 阅读1分钟
      <input type="text" v-model='num1' />
        <select v-model='ys'>
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="÷">÷</option>
        </select>
     <input type="text" v-model='num2' />={{getblock}}
     

image.png

   data: {
        num1: '',
        ys: '+',
        num2: '',
        result: '',
    },
    computed: {
        getblock() {
            switch (this.ys) {
                case '+':
                    this.result = Number(this.num1) + Number(this.num2);
                    break;
                case '-':
                    this.result = Number(this.num1) - Number(this.num2);
                    break;
                case '*':
                    this.result = Number(this.num1) * Number(this.num2);
                    break;
                case '÷':
                    this.result = Number(this.num1) / Number(this.num2);
                    break;
            }
            return this.result
        }
    }