久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Vue實現(xiàn)手機計算器

瀏覽:109日期:2022-12-05 13:58:39

本文實例為大家分享了Vue制作仿手機計算器的具體代碼,供大家參考,具體內(nèi)容如下

1.首先是把樣式做出來,按鈕是0-9,還有加減乘除,百分號,清除按鈕,小數(shù)點,等號、等等

Vue實現(xiàn)手機計算器

2.把官方網(wǎng)站的JS插件引用,cn.vuejs.org/v2/guide/

Vue實現(xiàn)手機計算器

頁面視圖

Vue實現(xiàn)手機計算器

JS

Vue實現(xiàn)手機計算器

Vue實現(xiàn)手機計算器

Vue實現(xiàn)手機計算器

new Vue({ el: '#app', data: { equation: ’0’, isDecimalAdded: false, //防止在一組數(shù)字中間輸入超過一個小數(shù)位 isOperatorAdded: false, //判斷之否已點擊 加、減、乘、除,防止連續(xù)點擊超過一個運算符號 isStarted: false, //判斷計算器是否已經(jīng)開始輸入數(shù)字,用于正負數(shù)和百分比計算的時候作一些判斷 }, methods: { //Check if the character is + - × ÷ isOperator(character) { //用來判斷character 是否加減乘除 return [’+’, ’-’, ’×’, ’÷’].indexOf(character) > -1 }, append(character) { //append(character)加減乘除 if (this.equation === ’0’ && !this.isOperator(character)) { if (character === ’.’) { this.equation += ’’ + character this.isDecimalAdded = true } else { this.equation = ’’ + character } this.isStarted = true return } if (!this.isOperator(character)) { if (character === ’.’ && this.isDecimalAdded) { return } if (character === ’.’) { this.isDecimalAdded = true this.isOperatorAdded = true } else { this.isOperatorAdded = false } this.equation += ’’ + character } if (this.isOperator(character) && !this.isOperatorAdded) { this.equation += ’’ + character this.isDecimalAdded = false this.isOperatorAdded = true } }, calculate() { //等于號的時候 let result = this.equation.replace(new RegExp(’×’, ’g’), ’*’).replace(new RegExp(’÷’, ’g’), ’/’) this.equation = parseFloat(eval(result).toFixed(9)).toString() this.isDecimalAdded = false this.isOperatorAdded = false }, calculateToggle() { //點擊正負號 if (this.isOperatorAdded || !this.isStarted) { true } this.equation = this.equation + ’* -1’ this.calculate() }, calculatePercentage() { //點擊百分比 if (this.isOperatorAdded || !this.isStarted) { true } this.equation = this.equation + ’* 0.01’ this.calculate() }, clear() { //點擊AC this.equation = ’0’ this.isDecimalAdded = false //防止在一組數(shù)字中間輸入超過一個小數(shù)位 this.isOperatorAdded = false //判斷之否已點擊 加、減、乘、除,防止連續(xù)點擊超過一個運算符號 this.isStarted = false //判斷計算器是否已經(jīng)開始輸入數(shù)字,用于正負數(shù)和百分比計算的時候作一些判斷 } }})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 西充县| 崇义县| 石狮市| 衡东县| 绥德县| 天峻县| 和平县| 隆德县| 济宁市| 青河县| 西安市| 田阳县| 瑞昌市| 固镇县| 津南区| 马关县| 桦南县| 通海县| 达日县| 竹山县| 灵武市| 镇远县| 湘阴县| 咸宁市| 中方县| 观塘区| 马关县| 墨江| 区。| 左权县| 合水县| 伊春市| 繁峙县| 疏勒县| 巴林右旗| 武山县| 永泰县| 长岭县| 定远县| 区。| 高淳县|