本文实例为大家分享了vue-model实现简易计算器的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < meta http-equiv = "X-UA-Compatible" content = "ie=edge" > < title >Vue</ title > < script src = "../lib/vue-2.4.0.js" ></ script > </ head > < body > < div id = "app" > <!-- 数字一 --> < input type = "text" v-model = 'n1' placeholder = "0" > <!-- 加减乘除 --> < select v-model = 'opt' > < option value = "+" > + </ option > < option value = "-" >-</ option > < option value = "*" >*</ option > < option value = "/" >/</ option > </ select > <!-- 数字2 --> < input type = "text" v-model = 'n2' placeholder = "0" > <!-- 等号 --> < input type = "button" value = '=' > <!-- 结果 --> < input type = "text" v-model = 'result' placeholder = "0" > <!-- 确定按钮 --> < input type = "button" value = '结果' @ click = 'calc' > <!-- 归零 --> < input type = "button" value = '归零' @ click = 'zero' > </ div > < script > var vm = new Vue({ el: '#app', //表示当前new的这个实例要控制页面上的那个区域 data: { //data属性存放着 el 中要用到的数据 n1: '', n2:'', result:'', opt: '+' }, methods:{ calc(){ // switch(this.opt){ // case '+': // this.result = parseInt(this.n1) + parseInt(this.n2) // break; // case '-': // this.result = parseInt(this.n1) - parseInt(this.n2) // break; // case '*': // this.result = parseInt(this.n1) * parseInt(this.n2) // break; // case '/': // this.result = parseInt(this.n1) / parseInt(this.n2) // break; // } // 简写 var codeStr = 'parseInt(this.n1) '+ this.opt +' parseInt(this.n2)' this.result = eval(codeStr) }, zero(){ this.n1 = '', this.n2 = '', this.result = '', this.opt = '+' } } }) </ script > </ body > </ html > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44777678/article/details/95006618