1. import Vue from 'vue';
    2. import {Component} from 'vue-property-decorator';
    3. @Component
    4. export default class NumberPad extends Vue {
    5. output = '0'; // 输出的数字为字符串类型
    6. outputChange(event: MouseEvent) {
    7. // vue会读取div的内容
    8. const input = event.target.textContent;
    9. if (this.output.length === 16) {return;}
    10. if (this.output === '0') {
    11. if ('0123456789'.indexOf(input) >= 0) {
    12. this.output = input;
    13. } else {
    14. this.output += input;
    15. }
    16. return
    17. }
    18. if (this.output.indexOf('.') >= 0 && input === '.') {
    19. return;
    20. }
    21. this.output += input;
    22. }
    23. deleteLast(){
    24. if(this.output.length === 1) {
    25. this.output = '0'
    26. } else {
    27. this.output = this.output.slice(0,-1) // -1表示最后一个元素
    28. }
    29. }
    30. clear(){
    31. this.output = '0'
    32. }
    33. }
    34. </script>

    如果webstorm提示你某个值可能为null,可强制指定类型

    1. // 1. 用 as
    2. event.target as HTMLButtonElement
    3. // 2. 用感叹号
    4. event.target!