Vue简单计算器
步骤:
- 通过v-model指令实现数值a和数值b的绑定
- 给计算按钮绑定事件,实现计算逻辑
- 将计算结果绑定到对应位子
代码
跟着网上视频打的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h1>简单计算器</h1>
<div>
<span>数值a:</span>
<span><input type="text" v-model='a' ></span>
</div>
<div>
<span>数值b:</span>
<span><input type="text" v-model='b' ></span>
</div>
<div>
<button v-on:click='handle'>计算</button>
</div>
<div>
<span>计算结果:</span>
<span v-text='result'></span>
</div>
</div>
<script src="../js/vue.js"></script>
<script type="text/javascript">
let app = new Vue({
el: '#app',
data: {
a: '',
b: '',
result: ''
},
methods: {
handle: function() {
this.result = parseInt(this.a) + parseInt(this.b)
}
}
})
</script>
</body>
</html>
|