使用Vue編寫(xiě)簡(jiǎn)單計(jì)算器,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、湄潭網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為湄潭等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
在Vue中,v-model 指令,可以實(shí)現(xiàn)表單元素和 Model 中數(shù)據(jù)的雙向數(shù)據(jù)綁定,接下來(lái),我們就用這個(gè)指令編寫(xiě)一個(gè)簡(jiǎn)單的計(jì)算器,代碼如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <!--計(jì)算器的顯示結(jié)構(gòu)--> <div id="app"> <input type="text" v-model="n1"> <select v-model="opt"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" v-model="n2"> <input type="button" value="=" @click="calculator"> <input type="text" v-model="result"> </div> <script> // 創(chuàng)建 Vue 實(shí)例,得到 ViewModel,簡(jiǎn)寫(xiě)vm var vm = new Vue({ el: '#app', data: { n1: 0, n2: 0, opt: '+', result: 0 }, methods: { //計(jì)算的方法 calculator() { switch (this.opt) { case '+': this.result = Number(this.n1) + Number(this.n2); break; case '-': this.result = Number(this.n1) - Number(this.n2); break; case '*': this.result = Number(this.n1) * Number(this.n2); break; case '/': this.result = Number(this.n1) / Number(this.n2); break; } } } }); </script> </body> </html>
運(yùn)行結(jié)果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章標(biāo)題:使用Vue實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
URL標(biāo)題:http://vcdvsql.cn/article28/gjogjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站改版、網(wǎng)站排名、網(wǎng)站制作、網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷(xiāo)型網(wǎng)站建設(shè)知識(shí)