Vue實(shí)現(xiàn)計(jì)數(shù)器案例
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)計(jì)數(shù)器展示的具體代碼,供大家參考,具體內(nèi)容如下
效果:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>計(jì)數(shù)器</title> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script> <style type='text/css'> #app{ text-align: center; margin: 0 auto; line-height: 500px; } #app input{ width: 50px; height: 40px; font-size: 20px; border-radius: 5px; outline: none; /* 自定義邊框 */ border: 1px solid transparent; background-color: blue; line-height: 30px; color: white; } #app span{ padding: 20px 20px; border: 1px; } </style></head><body> <div id='app'> <input type='button' value='-' @click='sub'/> <span>{{num}}</span> <input type='button' value='+' @click='add'/> </div> <script>var app = new Vue({ el: '#app', data: {num: 1 }, methods:{add: function(){ if(this.num<10){ this.num++; }else{ alert('達(dá)到最大啦!'); } },sub: function(){ if(this.num>0){this.num--; }else{alert('已經(jīng)沒(méi)有了!'); } } }}) </script></body></html> data中寫(xiě)需要用到的數(shù)據(jù): num -methods中添加兩個(gè)方法:加(add)、減(sub) 使用v-text或者差值表達(dá)式將num設(shè)置給span標(biāo)簽 使用v-on:(簡(jiǎn)寫(xiě),@)將add、sub分別綁定給+、-按鈕 累加的邏輯:小于10累加,否則提示 遞減的邏輯:大于0遞漸,否則提示 方法中通過(guò)this關(guān)鍵字獲取data中的數(shù)據(jù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Android studio 動(dòng)態(tài)fragment的用法2. Android如何加載Base64編碼格式圖片3. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題4. 圖文詳解vue中proto文件的函數(shù)調(diào)用5. 什么是python的自省6. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)7. Vuex localStorage的具體使用8. php模擬實(shí)現(xiàn)斗地主發(fā)牌9. vue 使用localstorage實(shí)現(xiàn)面包屑的操作10. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備