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

案例思路
1、通過(guò)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)頁(yè)面
<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)頁(yè)面添加指令
<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)頁(yè)計(jì)算器
相關(guān)文章
vue中循環(huán)多個(gè)li(表格)并獲取對(duì)應(yīng)的ref的操作代碼
我想要獲取每一個(gè)循環(huán)并獲取每一個(gè)li(或者其它循環(huán)項(xiàng))的ref,以便于后續(xù)的操作,接下來(lái)通過(guò)本文給大家分享vue中循環(huán)多個(gè)li(表格)并獲取對(duì)應(yīng)的ref的操作代碼,感興趣的朋友跟隨小編一起看看吧2024-02-02
Vue路由跳轉(zhuǎn)傳參或者打開(kāi)新頁(yè)面跳轉(zhuǎn)問(wèn)題
這篇文章主要介紹了Vue路由跳轉(zhuǎn)傳參或者打開(kāi)新頁(yè)面跳轉(zhuǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
vue如何通過(guò)props方式在子組件中獲取相應(yīng)的對(duì)象
這篇文章主要介紹了vue如何通過(guò)props方式在子組件中獲取相應(yīng)的對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue3.x+Element Plus仿制Acro Design簡(jiǎn)潔模式實(shí)現(xiàn)分頁(yè)器組件
開(kāi)發(fā)中難免會(huì)遇到寬度很窄的列表需要使用分頁(yè)器的情況。本文將利用Vue3.x+Element Plus仿制Acro Design簡(jiǎn)潔模式實(shí)現(xiàn)分頁(yè)器組件,感興趣的可以了解一下2023-02-02
vue 中 element-ui table合并上下兩行相同數(shù)據(jù)單元格
這篇文章主要介紹了vue 中 element-ui table合并上下兩行相同數(shù)據(jù)單元格,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
Vue中的v-for循環(huán)key屬性注意事項(xiàng)小結(jié)
這篇文章主要介紹了Vue中的v-for循環(huán)key屬性注意事項(xiàng)小結(jié),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Vue v-html指令詳細(xì)解析與代碼實(shí)例(最新推薦)
v-html是Vue.js框架中的一個(gè)指令,用于將數(shù)據(jù)中的HTML代碼動(dòng)態(tài)渲染到頁(yè)面上,它主要用于渲染一些靜態(tài)的HTML內(nèi)容或者從后臺(tái)獲取的富文本數(shù)據(jù),下面介紹Vue v-html詳細(xì)解析與代碼實(shí)例,感興趣的朋友一起看看吧2024-12-12

