Vue實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
案例需求

案例思路
1、通過v-model 指令 實(shí)現(xiàn)數(shù)值A(chǔ)和數(shù)值B的綁定
2、給計(jì)算按鈕綁定事件,實(shí)現(xiàn)計(jì)算邏輯
3、將計(jì)算結(jié)果綁定到對(duì)應(yīng)位置
實(shí)現(xiàn)靜態(tài)頁面
<div id='app'>
<h1>簡(jiǎn)單計(jì)算器</h1>
<div><span>數(shù)值A(chǔ):</span><span><input type="text" v-model='a'></span></div>
<div><span>數(shù)值B:</span><span type="text" v-model='b'></span></div>
<div><button>計(jì)算</button></div>
<div><span>計(jì)算結(jié)果</span><span></span></div>
</div>
導(dǎo)入Vue
<script type="text/javascript" src="js/vue.js"></script>
為靜態(tài)頁面添加指令
<div id='app'>
<h1>簡(jiǎn)單計(jì)算器</h1>
<div><span>數(shù)值A(chǔ):</span>
<span>
<input type="text" v-model='a'>
</span>
</div>
<div>
<span>數(shù)值B:</span>
<span>
<input type="text" v-model='b'>
</span>
</div>
<div>
<button v-on:click="handle">計(jì)算</button>
</div>
<div><span>計(jì)算結(jié)果</span><span v-text="result"></span></div>
</div>
設(shè)置 計(jì)算功能
<script type="text/javascript">
/* */
var vm = new Vue({
el: "#app",
data: {
a: '',
b: '',
result: ''
},
methods: {
handle: function () {
// 實(shí)現(xiàn)計(jì)算邏輯
this.result = parseInt(this.a) + parseInt(this.b);
}
}
});
</script>
最終代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>簡(jiǎn)單計(jì)算器</title>
</head>
<body>
<div id='app'>
<h1>簡(jiǎn)單計(jì)算器</h1>
<div><span>數(shù)值A(chǔ):</span>
<span>
<input type="text" v-model='a'>
</span>
</div>
<div>
<span>數(shù)值B:</span>
<span>
<input type="text" v-model='b'>
</span>
</div>
<div>
<button v-on:click="handle">計(jì)算</button>
</div>
<div><span>計(jì)算結(jié)果</span><span v-text="result"></span></div>
</div>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript">
/* */
var vm = new Vue({
el: "#app",
data: {
a: '',
b: '',
result: ''
},
methods: {
handle: function () {
// 實(shí)現(xiàn)計(jì)算邏輯
this.result = parseInt(this.a) + parseInt(this.b);
}
}
});
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue.js實(shí)現(xiàn)的計(jì)算器功能完整示例
- 使用Vue實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- vue.js實(shí)現(xiàn)的經(jīng)典計(jì)算器/科學(xué)計(jì)算器功能示例
- vue實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
- Vue.js實(shí)現(xiàn)價(jià)格計(jì)算器功能
- vue實(shí)現(xiàn)計(jì)算器功能
- Vue實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- Vue實(shí)現(xiàn)手機(jī)計(jì)算器
- vue.js實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
- Vue實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁計(jì)算器
相關(guān)文章
詳解vue-cli與webpack結(jié)合如何處理靜態(tài)資源
本篇文章主要介紹了詳解vue-cli與webpack結(jié)合如何處理靜態(tài)資源,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
vue使用iview的modal彈窗嵌套modal出現(xiàn)格式錯(cuò)誤的解決
這篇文章主要介紹了vue使用iview的modal彈窗嵌套modal出現(xiàn)格式錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue利用路由鉤子token過期后跳轉(zhuǎn)到登錄頁的實(shí)例
下面小編就為大家?guī)硪黄猇ue利用路由鉤子token過期后跳轉(zhuǎn)到登錄頁的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Vue使用vm.$set()解決對(duì)象新增屬性不能響應(yīng)的問題
這篇文章主要介紹了Vue使用vm.$set()解決對(duì)象新增屬性不能響應(yīng)的問題,為了解決這個(gè)問題,Vue提供了一個(gè)特殊的方法vm.$set(object, propertyName, value),也可以使用全局的Vue.set(object, propertyName, value)方法,需要的朋友可以參考下2023-05-05
Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能
這篇文章主要為大家詳細(xì)介紹了Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
關(guān)于vue跳轉(zhuǎn)后頁面置頂?shù)膯栴}
這篇文章主要介紹了關(guān)于vue跳轉(zhuǎn)后頁面置頂?shù)膯栴},具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

