Blogger Information
Blog 59
fans 6
comment 0
visits 51917
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue:计算器的加减乘除-vue-52课9.8
希望
Original
2498 people have browsed it

1、Vue:计算器的加减乘除

  • 需要下载vue.js 并安装到VSCode里,并引用到页面中。网址:https://cn.vuejs.org/v2/guide/installation.html 找到开发版本下载

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>Vue:计算器的加减乘除</title>
    7. <script src="lib/vue.js"></script>
    8. <style>
    9. * {
    10. padding: 0;
    11. margin: 0;
    12. }
    13. .item {
    14. cursor: pointer;
    15. }
    16. .item:hover {
    17. background: rgb(243, 204, 204);
    18. }
    19. .container_box {
    20. display: grid;
    21. grid-template-rows: 80px 80px 80px;
    22. grid-template-columns: 80px 80px 80px;
    23. float: left;
    24. }
    25. .container_box .item {
    26. font-size: 24px;
    27. text-align: center;
    28. border: 1px solid rgb(194, 194, 194);
    29. line-height: 80px;
    30. color: #333;
    31. }
    32. .btns {
    33. display: grid;
    34. grid-template-rows: 80px 80px 80px;
    35. }
    36. .btns .item {
    37. font-size: 24px;
    38. text-align: center;
    39. border: 1px solid rgb(194, 194, 194);
    40. line-height: 80px;
    41. color: #333;
    42. width: 80px;
    43. }
    44. .res {
    45. width: 153px;
    46. height: 30px;
    47. border: 3px solid rgb(154, 128, 240);
    48. line-height: 30px;
    49. text-align: center;
    50. cursor: pointer;
    51. cursor: pointer;
    52. display: inline-block;
    53. margin-top: 5px;
    54. }
    55. .res:hover {
    56. background: rgb(215, 243, 149);
    57. }
    58. .sign {
    59. margin-top: 20px;
    60. font-size: 28px;
    61. font-weight: bold;
    62. padding: 15px;
    63. color: rgb(226, 38, 38);
    64. }
    65. </style>
    66. </head>
    67. <body>
    68. <div class="app">
    69. <div class="container_box" @click="getValue">
    70. <div class="item">7</div>
    71. <div class="item">8</div>
    72. <div class="item">9</div>
    73. <div class="item">4</div>
    74. <div class="item">5</div>
    75. <div class="item">6</div>
    76. <div class="item">1</div>
    77. <div class="item">2</div>
    78. <div class="item">3</div>
    79. <div class="item">0</div>
    80. <div class="item" style="width: 158px">.</div>
    81. </div>
    82. <div class="btns" @click="getSign">
    83. <div class="item">+</div>
    84. <div class="item">-</div>
    85. <div class="item">*</div>
    86. <div class="item">/</div>
    87. </div>
    88. <div class="res" @click="init">清除</div>
    89. <div class="res" @click="getRes">计算</div>
    90. <div class="sign">
    91. <span>{{num1}}</span>
    92. <span>{{sign}}</span>
    93. <span>{{num2}}</span>
    94. <span>{{equal}}</span>
    95. <span>{{res}}</span>
    96. </div>
    97. </div>
    98. <script>
    99. // 获取挂载点
    100. var vm = new Vue({
    101. el: ".app",
    102. data: {
    103. num1: "",
    104. num2: "",
    105. sign: "",
    106. res: "",
    107. flag: true,
    108. equal: "",
    109. },
    110. methods: {
    111. //获取数字
    112. getValue(e) {
    113. if (this.flag == true) {
    114. this.num1 += e.target.innerText;
    115. }
    116. if (this.flag == false) {
    117. this.num2 += e.target.innerText;
    118. }
    119. },
    120. //获取运算符
    121. getSign(e) {
    122. this.flag = false;
    123. this.sign = e.target.innerText;
    124. },
    125. //计算
    126. getRes() {
    127. this.equal = "=";
    128. switch (this.sign) {
    129. case "+":
    130. this.res = this.num1 * 1 + this.num2 * 1;
    131. break;
    132. case "-":
    133. this.res = this.num1 - this.num2;
    134. break;
    135. case "*":
    136. this.res = this.num1 * this.num2;
    137. break;
    138. case "/":
    139. this.res = this.num1 / this.num2;
    140. break;
    141. default:
    142. this.res = "输入错误";
    143. break;
    144. }
    145. },
    146. init() {
    147. this.flag = true;
    148. this.num1 = "";
    149. this.num2 = "";
    150. this.sign = "";
    151. this.res = "";
    152. this.equal = "";
    153. },
    154. },
    155. });
    156. </script>
    157. </body>
    158. </html>
  • 总结:
  • Vue:可以从后台获取的数据并能动态展示在页面上,能够计算属性和结果
  • 还可以充当过滤器,还能进行样式的修改
  • 数组 、对象、对象数组的遍历
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:完成的很不错嘛
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments