Vue實(shí)現(xiàn)手機(jī)計(jì)算器
本文實(shí)例為大家分享了Vue制作仿手機(jī)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
1.首先是把樣式做出來,按鈕是0-9,還有加減乘除,百分號,清除按鈕,小數(shù)點(diǎn),等號、等等

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

頁面視圖

JS



new Vue({
el: "#app",
data: {
equation: '0',
isDecimalAdded: false, //防止在一組數(shù)字中間輸入超過一個小數(shù)位
isOperatorAdded: false, //判斷之否已點(diǎn)擊 加、減、乘、除,防止連續(xù)點(diǎn)擊超過一個運(yùn)算符號
isStarted: false, //判斷計(jì)算器是否已經(jīng)開始輸入數(shù)字,用于正負(fù)數(shù)和百分比計(jì)算的時候作一些判斷
},
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() { //點(diǎn)擊正負(fù)號
if (this.isOperatorAdded || !this.isStarted) {
true
}
this.equation = this.equation + '* -1'
this.calculate()
},
calculatePercentage() { //點(diǎn)擊百分比
if (this.isOperatorAdded || !this.isStarted) {
true
}
this.equation = this.equation + '* 0.01'
this.calculate()
},
clear() { //點(diǎn)擊AC
this.equation = '0'
this.isDecimalAdded = false //防止在一組數(shù)字中間輸入超過一個小數(shù)位
this.isOperatorAdded = false //判斷之否已點(diǎn)擊 加、減、乘、除,防止連續(xù)點(diǎn)擊超過一個運(yùn)算符號
this.isStarted = false //判斷計(jì)算器是否已經(jīng)開始輸入數(shù)字,用于正負(fù)數(shù)和百分比計(jì)算的時候作一些判斷
}
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例
- 使用Vue實(shí)現(xiàn)簡單計(jì)算器
- vue.js實(shí)現(xiàn)的經(jīng)典計(jì)算器/科學(xué)計(jì)算器功能示例
- vue實(shí)現(xiàn)簡單加法計(jì)算器
- vue實(shí)現(xiàn)簡易的計(jì)算器功能
- Vue.js實(shí)現(xiàn)價格計(jì)算器功能
- Vue實(shí)現(xiàn)簡易計(jì)算器
- vue實(shí)現(xiàn)計(jì)算器功能
- vue.js實(shí)現(xiàn)簡單的計(jì)算器功能
- vue實(shí)現(xiàn)簡單的計(jì)算器功能
相關(guān)文章
Vue實(shí)現(xiàn)購物車場景下的應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)購物車場景下的應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
vue使用keep-alive實(shí)現(xiàn)數(shù)據(jù)緩存不刷新
這篇文章主要介紹了vue使用keep-alive實(shí)現(xiàn)數(shù)據(jù)緩存不刷新,這里整理了詳細(xì)的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
vue源碼解讀子節(jié)點(diǎn)優(yōu)化更新
這篇文章主要為大家介紹了vue源碼解讀子節(jié)點(diǎn)優(yōu)化更新示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總
這篇文章主要介紹了html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-11-11
vue arco.design錨點(diǎn)Anchor使用方式
這篇文章主要介紹了vue arco.design錨點(diǎn)Anchor使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
vue中使用go()和back()兩種返回上一頁的區(qū)別說明
這篇文章主要介紹了vue中使用go()和back()兩種返回上一頁的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
關(guān)于Vue?ui?的沒反應(yīng)、報錯問題解決總結(jié)
這篇文章主要介紹了關(guān)于Vue?ui?的沒反應(yīng)、報錯問題解決總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

