Vue学习笔记01day_06.简易计算器

淩亂°似流年 2022-05-13 06:12 338阅读 0赞
  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <script src="./lib/vue-2.4.0.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <input type="text" v-model="n1">
  13. <select v-model="opt">
  14. <option value="+">+</option>
  15. <option value="-">-</option>
  16. <option value="*">*</option>
  17. <option value="/">/</option>
  18. </select>
  19. <input type="text" v-model="n2">
  20. <input type="button" value="=" @click="calc">
  21. <input type="text" v-model="result">
  22. </div>
  23. <script>
  24. // 创建 Vue 实例,得到 ViewModel
  25. var vm = new Vue({
  26. el: '#app',
  27. data: {
  28. n1: 0,
  29. n2: 0,
  30. result: 0,
  31. opt: '+'
  32. },
  33. methods: {
  34. calc() { // 计算器算数的方法
  35. // 逻辑:
  36. /* switch (this.opt) {
  37. case '+':
  38. this.result = parseInt(this.n1) + parseInt(this.n2)
  39. break;
  40. case '-':
  41. this.result = parseInt(this.n1) - parseInt(this.n2)
  42. break;
  43. case '*':
  44. this.result = parseInt(this.n1) * parseInt(this.n2)
  45. break;
  46. case '/':
  47. this.result = parseInt(this.n1) / parseInt(this.n2)
  48. break;
  49. } */
  50. // 注意:这是投机取巧的方式,正式开发中,尽量少用
  51. var codeStr = 'parseInt(this.n1) ' + this.opt + ' parseInt(this.n2)'
  52. this.result = eval(codeStr)
  53. }
  54. }
  55. });
  56. </script>
  57. </body>
  58. </html>

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读

    相关 day06_01

    ArrayList集合的使用 \   也是引用数据类型 \ 步骤: \    1,导入包java.util包中 \    2.创建引用类型的变量 \