欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現(xiàn)計算器封裝

 更新時間:2022年04月15日 13:51:24   作者:@Herry  
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)計算器的封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)計算器封裝代碼,供大家參考,具體內(nèi)容如下

前言:根據(jù)計算器可增添加減乘除的封裝可擴展,大家請參照效果如下:

文件目錄

我們導(dǎo)入了四個js包,在下面有源代碼,當(dāng)前計算器頁只有一個valculator.vue文件。

valculator.vue:<html代碼>

template>
? <div class="about">
? ? <h1>這是個計算器你信嗎</h1>
? ? <van-cell-group type="text">
? ? ? <van-field
? ? ? ? οninput="value=value.replace(/[^\d+(+)+(-)+(*)]/g, '').replace(/^0{1,}/g,'')"
? ? ? ? v-model="inputValue"
? ? ? ? placeholder="請輸入數(shù)字"
? ? ? />
? ? </van-cell-group>
? ? <div style="margin-top:20%">
? ? ? <van-grid clickable :column-num="4" :gutter="10">
? ? ? ? <van-grid-item @click="onclick(i)" v-for="(num, i) in dataNum" :key="i" :text="dataNum[i]" />
? ? ? </van-grid>
? ? </div>
? </div>
</template>

valculator.vue:《js方法》

<script>
// eslint-disable-next-line no-unused-vars
import { Field } from 'vant'
export default {
? data () {
? ? return {
? ? ? // 數(shù)字加減乘除數(shù)組
? ? ? dataNum: [
? ? ? ? '+',
? ? ? ? '-',
? ? ? ? '*',
? ? ? ? '/',
? ? ? ? '1',
? ? ? ? '2',
? ? ? ? '3',
? ? ? ? '< X',
? ? ? ? '4',
? ? ? ? '5',
? ? ? ? '6',
? ? ? ? '=',
? ? ? ? '7',
? ? ? ? '8',
? ? ? ? '9',
? ? ? ? '0'
? ? ? ],
? ? ? inputValue: '', // input當(dāng)前顯示值
? ? ? inputStorage: '', // input輸入值存儲
? ? ? calculator: '', // 解析拿到的值

? ? ? temporaryVariables1: '', // 存儲臨時計算拼接值1
? ? ? temporaryVariables2: '', // 存儲臨時計算拼接值2
? ? ? temporaryOperator: '' // 存儲臨時運算符
? ? }
? },
? methods: {
? ? // 點擊事件
? ? onclick (index) {
? ? ? this.parsing(index) // 解析當(dāng)前的值
? ? ? this.judge() // 判斷進(jìn)行運算
? ? },

? ? // 解析當(dāng)前拿到的值
? ? parsing (index) {
? ? ? switch (index) {
? ? ? ? case 4:
? ? ? ? ? this.calculator = '1'
? ? ? ? ? break
? ? ? ? case 5:
? ? ? ? ? this.calculator = '2'
? ? ? ? ? break
? ? ? ? case 6:
? ? ? ? ? this.calculator = '3'
? ? ? ? ? break
? ? ? ? case 8:
? ? ? ? ? this.calculator = '4'
? ? ? ? ? break
? ? ? ? case 9:
? ? ? ? ? this.calculator = '5'
? ? ? ? ? break
? ? ? ? case 10:
? ? ? ? ? this.calculator = '6'
? ? ? ? ? break
? ? ? ? case 12:
? ? ? ? ? this.calculator = '7'
? ? ? ? ? break
? ? ? ? case 13:
? ? ? ? ? this.calculator = '8'
? ? ? ? ? break
? ? ? ? case 14:
? ? ? ? ? this.calculator = '9'
? ? ? ? ? break
? ? ? ? case 15:
? ? ? ? ? this.calculator = '0'
? ? ? ? ? break
? ? ? ? case 0:
? ? ? ? ? this.calculator = '+'
? ? ? ? ? break
? ? ? ? case 1:
? ? ? ? ? this.calculator = '-'
? ? ? ? ? break
? ? ? ? case 2:
? ? ? ? ? this.calculator = '*'
? ? ? ? ? break
? ? ? ? case 3:
? ? ? ? ? this.calculator = '/'
? ? ? ? ? break
? ? ? ? case 11:
? ? ? ? ? this.calculator = '='
? ? ? ? ? break
? ? ? ? case 7:
? ? ? ? ? this.calculator = 'X'
? ? ? ? ? this.Clear()
? ? ? ? ? break
? ? ? ? default:
? ? ? ? ? break
? ? ? }

? ? ? // ? this.outValue = this.calculator;
? ? ? // ? this.inputBox();
? ? ? // ? console.log(this.calculator);
? ? },

? ? // 判斷是哪個運算符
? ? judge () {
? ? ? if (this.calculator === '=') {
? ? ? ? this.equal()
? ? ? } else if (this.calculator === 'X') {
? ? ? ? this.Clear()
? ? ? } else {
? ? ? ? this.showOn() // 顯示當(dāng)前的值
? ? ? ? this.calculation() // 計算當(dāng)前的值
? ? ? }
? ? },

? ? // 計算當(dāng)前的值
? ? calculation () {
? ? ? // 如果為空表示當(dāng)前為第一個運算符,否則開始計算

? ? ? const vae = this.isNumber(this.calculator) // 判斷當(dāng)前輸入值是否為數(shù)字
? ? ? if (this.temporaryOperator === '') {
? ? ? ? if (vae === false) {
? ? ? ? ? this.temporaryOperator = this.calculator // 存儲當(dāng)前計算值
? ? ? ? } else {
? ? ? ? ? this.temporaryVariables1 += this.calculator // 計算的值:加減觸發(fā)前拼接的值
? ? ? ? }
? ? ? } else {
? ? ? ? if (vae === false) {
? ? ? ? ? this.temporaryVariables1 = this.Retrieval.retrieval(
? ? ? ? ? ? this.temporaryOperator,
? ? ? ? ? ? this.temporaryVariables1,
? ? ? ? ? ? this.temporaryVariables2
? ? ? ? ? ) // 如果當(dāng)前有輸入運算法調(diào)取加減乘除

? ? ? ? ? this.assignmentConversion() // 清空第二個數(shù)
? ? ? ? ? this.temporaryOperator = this.calculator // 計算完后保留當(dāng)前的運算符
? ? ? ? } else {
? ? ? ? ? this.temporaryVariables2 += this.calculator // 繼續(xù)第二個拼接
? ? ? ? }
? ? ? }
? ? },

? ? // 判斷是否為數(shù)字:“12.3”等都為true, “哈哈”、“12.33”等都為false
? ? isNumber (val) {
? ? ? var regPos = /^\d+(\.\d+)?$/ // 非負(fù)浮點數(shù)
? ? ? var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/ // 負(fù)浮點數(shù)
? ? ? if (regPos.test(val) || regNeg.test(val)) {
? ? ? ? return true
? ? ? } else {
? ? ? ? return false
? ? ? }
? ? },

? ? // 等于
? ? equal () {
? ? ? this.temporaryVariables1 = this.Retrieval.retrieval(
? ? ? ? this.temporaryOperator,
? ? ? ? this.temporaryVariables1,
? ? ? ? this.temporaryVariables2
? ? ? ) // 調(diào)取加減乘除
? ? ? this.assignmentConversion() // 清空第二個數(shù)
? ? ? this.inputValue = this.temporaryVariables1 // 將計算后的值顯示在屏幕上
? ? ? this.inputStorage = '' // 清空之前存儲的值
? ? },

? ? // 清空第二個數(shù)
? ? assignmentConversion () {
? ? ? this.temporaryVariables2 = '' // 第二個清空用來再次保留
? ? },

? ? // 清除顯示的數(shù)據(jù)
? ? Clear () {
? ? ? this.inputValue = '' // 顯示的值
? ? ? this.inputStorage = '' // input輸入值存儲
? ? ? this.calculator = '' // 解析拿到的值

? ? ? this.temporaryVariables1 = '' // 存儲臨時計算拼接值1
? ? ? this.temporaryVariables2 = '' // 存儲臨時計算拼接值2
? ? ? this.temporaryOperator = '' // 存儲臨時運算符
? ? },

? ? // 顯示當(dāng)前的值
? ? showOn () {
? ? ? this.inputValue = this.inputStorage // 之前存儲先賦給要顯示的
? ? ? this.inputValue += this.calculator // 要顯示的值再次加上當(dāng)前點擊的值
? ? ? this.inputStorage = this.inputValue // 同步要存儲的值
? ? }
? }
}

valculator.vue:《style》

<style scoped>
div.inputAll {
? position: relative;
}

div.inputOne {
? position: absolute;
? top: 10%;
? /* border-bottom:1px solid gray; */
}

div.inputTwo {
? position: absolute;
? top: 15%;
}

div.inputLine {
? border-bottom: 1px solid gray;
? top: 12.5%;
? position: absolute;
}
</style>

導(dǎo)入其他js文件:

retrieval.js:計算器加減乘除選擇器

// eslint-disable-next-line no-unused-vars
import Add from '../valculator/add'
// eslint-disable-next-line no-unused-vars
import Subtraction from '../valculator/subtraction'
import Multiplication from '../valculator/multiplication'
export default {
? retrieval: function (operator, variables1, variables2) {
? ? switch (operator) {
? ? ? case '+':
? ? ? ? // 調(diào)取公共加法
? ? ? ? // eslint-disable-next-line no-undef
? ? ? ? variables1 = Add.add(variables1, variables2)
? ? ? ? break
? ? ? case '-':
? ? ? ? // 調(diào)取公共減法
? ? ? ? // eslint-disable-next-line no-undef
? ? ? ? variables1 = Subtraction.subtraction(variables1, variables2)
? ? ? ? break
? ? ? // eslint-disable-next-line no-duplicate-case
? ? ? case '*':
? ? ? ? // 調(diào)取公共乘法
? ? ? ? // eslint-disable-next-line no-undef
? ? ? ? variables1 = Multiplication.multiplication(variables1, variables2)
? ? ? ? break
? ? ? default:
? ? ? ? break
? ? }
? ? return variables1
? }
}

add.js:加法操作

export default {
? add: function (addOne, addTwo) {
? ? // eslint-disable-next-line no-unused-vars
? ? addOne = Number(addOne) + Number(addTwo) // 顯示當(dāng)前的值
? ? return addOne
? }
}

multiplication.js:乘法操作

export default {
? multiplication: function (addOne, addTwo) {
? ? // eslint-disable-next-line no-unused-vars
? ? addOne = Number(addOne) * Number(addTwo) // 顯示當(dāng)前的值
? ? return addOne
? }
}

subtraction.js:減法操作

export default {
? subtraction: function (addOne, addTwo) {
? ? // eslint-disable-next-line no-unused-vars
? ? addOne = Number(addOne) - Number(addTwo) // 顯示當(dāng)前的值
? ? return addOne
? }
}

總結(jié):

我們對于加減同一級別的代碼可以自己添加一個js文件,然后retrieval.js里面寫進(jìn)入即可,當(dāng)然我們最好將這個js文件換成xml就可以實現(xiàn)仿反射+配置文件了,對于乘除法我們需要進(jìn)一步更改計算器為每次都是兩個計算,不可以一次性輸入很多數(shù)字,這樣可以避免開優(yōu)先級問題,當(dāng)然我們要做成優(yōu)先級是我們很重要的學(xué)習(xí)理論依據(jù)。

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

相關(guān)文章

  • vue 自定義icon圖標(biāo)的步驟

    vue 自定義icon圖標(biāo)的步驟

    這篇文章主要介紹了vue 自定義icon的圖標(biāo)的步驟,文中大概給大家分為兩步驟,通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • vue2.0 實現(xiàn)富文本編輯器功能

    vue2.0 實現(xiàn)富文本編輯器功能

    這篇文章主要介紹了vue2.0 實現(xiàn)富文本編輯器功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-05-05
  • vue-cli-service的參數(shù)配置過程

    vue-cli-service的參數(shù)配置過程

    這篇文章主要介紹了vue-cli-service的參數(shù)配置過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue極簡生成器?Vuepress的實現(xiàn)

    Vue極簡生成器?Vuepress的實現(xiàn)

    本文主要介紹了Vue極簡生成器?Vuepress的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • 淺談vue中改elementUI默認(rèn)樣式引發(fā)的static與assets的區(qū)別

    淺談vue中改elementUI默認(rèn)樣式引發(fā)的static與assets的區(qū)別

    下面小編就為大家分享一篇淺談vue中改elementUI默認(rèn)樣式引發(fā)的static 與assets的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • element-ui 文件上傳修改文件名的方法示例

    element-ui 文件上傳修改文件名的方法示例

    這篇文章主要介紹了element-ui 文件上傳修改文件名的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • vue中動態(tài)綁定表單元素的屬性方法

    vue中動態(tài)綁定表單元素的屬性方法

    下面小編就為大家分享一篇vue中動態(tài)綁定表單元素的屬性方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 解決vue-cli?卸載不掉的問題

    解決vue-cli?卸載不掉的問題

    這篇文章主要介紹了vue-cli?卸載不掉的問題解決方法,文中給大家介紹了安裝后問題分析及解決方案,需要的朋友可以參考下
    2023-01-01
  • vue項目中定時器無法清除的原因解決

    vue項目中定時器無法清除的原因解決

    頁面有定時器,并且定時器在離開頁面時,有清除,本文主要介紹了vue項目中定時器無法清除的原因解決,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Vue+Vuex實現(xiàn)自動登錄的知識點詳解

    Vue+Vuex實現(xiàn)自動登錄的知識點詳解

    在本篇文章里小編給大家整理的是關(guān)于Vue+Vuex實現(xiàn)自動登錄的知識點詳解,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03

最新評論