1.实例化vue对象
<script>
var vm = new Vue( {
el: '#app',
data: {
//输入的第一个数字
num1: '',
//输入的第二个数字
num2: '',
//运算符
opt: '+',
//方法计算结果
results: 0,
//监听属性计算结果
result: 0
},
2.用input接收用户输入的值,利用v-model对数据进行绑定
<body>
<div id="app">
<input type="text" placeholder="请输入第一个数字" v-model.number="num1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
<button @click="change">计算</button>
<br>方法结果:{{results}}
<br>计算属性结果:{{changes}}
<br>监听属性结果:{{result}}
3.使用方法实现计算器功能
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" placeholder="请输入第一个数字" v-model.number="num1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
<button @click="change">计算</button>
<br>方法结果:{{results}}
<br>计算属性结果:{{changes}}
<br>监听属性结果:{{result}}
</div>
<script>
var vm = new Vue( {
el: '#app',
data: {
//输入的第一个数字
num1: '',
//输入的第二个数字
num2: '',
//运算符
opt: '+',
//方法计算结果
results: 0,
//监听属性计算结果
result: 0
},
//方法实现计算器功能
methods: {
change ()
{
if ( this.opt == '+' )
{
this.results = this.num1 + this.num2;
} else if ( this.opt == '-' )
{
this.results = this.num1 - this.num2;
} else if ( this.opt == '*' )
{
this.results = this.num1 * this.num2;
} else if ( this.opt == '/' )
{
this.results = this.num1 / this.num2;
}
}
4.使用监听属性实现计算器功能
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" placeholder="请输入第一个数字" v-model.number="num1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
<button @click="change">计算</button>
<br>方法结果:{{results}}
<br>计算属性结果:{{changes}}
<br>监听属性结果:{{result}}
</div>
<script>
var vm = new Vue( {
el: '#app',
data: {
//输入的第一个数字
num1: '',
//输入的第二个数字
num2: '',
//运算符
opt: '+',
//方法计算结果
results: 0,
//监听属性计算结果
result: 0
},
//监听属性实现计算器功能
watch: {
opt:function ( val )
{
if ( val == '+' )
{
this.result = this.num1 + this.num2;
} else if ( val == '-' )
{
this.result = this.num1 - this.num2;
} else if ( val == '*' )
{
this.result = this.num1 * this.num2;
} else if ( val == '/' )
{
this.result = this.num1 / this.num2;
}
},
num1:function ( val )
{
if ( this.opt == '+' )
{
return this.result = val + this.num2;
} else if ( this.opt == '-' )
{
return this.result = val - this.num2;
} else if ( this.opt == '*' )
{
return this.result = val * this.num2;
} else if ( this.opt == '/' )
{
return this.result = val / this.num2;
}
},
num2:function ( val )
{
if ( this.opt == '+' )
{
this.result = this.num1 + val;
} else if ( this.opt == '-' )
{
this.result = this.num1 - val;
} else if ( this.opt == '*' )
{
this.result = this.num1 * val;
} else if ( this.opt == '/' )
{
this.result = this.num1 / val;
}
}
}
} )
</script>
</body>
</html>
5.使用计算属性实现计算器功能
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" placeholder="请输入第一个数字" v-model.number="num1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
<button @click="change">计算</button>
<br>方法结果:{{results}}
<br>计算属性结果:{{changes}}
<br>监听属性结果:{{result}}
</div>
<script>
var vm = new Vue( {
el: '#app',
data: {
//输入的第一个数字
num1: '',
//输入的第二个数字
num2: '',
//运算符
opt: '+',
//方法计算结果
results: 0,
//监听属性计算结果
result: 0
},
//计算属性实现计算器功能
computed: {
changes ()
{
if ( this.opt == '+' )
{
return this.result = this.num1 + this.num2;
} else if ( this.opt == '-' )
{
return this.result = this.num1 - this.num2;
} else if ( this.opt == '*' )
{
return this.result = this.num1 * this.num2;
} else if ( this.opt == '/' )
{
return this.result = this.num1 / this.num2;
}
}
},
</script>
6.完整代码如下
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
<script src="./js/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" placeholder="请输入第一个数字" v-model.number="num1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
<button @click="change">计算</button>
<br>方法结果:{{results}}
<br>计算属性结果:{{changes}}
<br>监听属性结果:{{result}}
</div>
<script>
var vm = new Vue( {
el: '#app',
data: {
//输入的第一个数字
num1: '',
//输入的第二个数字
num2: '',
//运算符
opt: '+',
//方法计算结果
results: 0,
//监听属性计算结果
result: 0
},
//方法实现计算器功能
methods: {
change ()
{
if ( this.opt == '+' )
{
this.results = this.num1 + this.num2;
} else if ( this.opt == '-' )
{
this.results = this.num1 - this.num2;
} else if ( this.opt == '*' )
{
this.results = this.num1 * this.num2;
} else if ( this.opt == '/' )
{
this.results = this.num1 / this.num2;
}
}
},
//计算属性实现计算器功能
computed: {
changes ()
{
if ( this.opt == '+' )
{
return this.result = this.num1 + this.num2;
} else if ( this.opt == '-' )
{
return this.result = this.num1 - this.num2;
} else if ( this.opt == '*' )
{
return this.result = this.num1 * this.num2;
} else if ( this.opt == '/' )
{
return this.result = this.num1 / this.num2;
}
}
},
//监听属性实现计算器功能
watch: {
opt:function ( val )
{
if ( val == '+' )
{
this.result = this.num1 + this.num2;
} else if ( val == '-' )
{
this.result = this.num1 - this.num2;
} else if ( val == '*' )
{
this.result = this.num1 * this.num2;
} else if ( val == '/' )
{
this.result = this.num1 / this.num2;
}
},
num1:function ( val )
{
if ( this.opt == '+' )
{
return this.result = val + this.num2;
} else if ( this.opt == '-' )
{
return this.result = val - this.num2;
} else if ( this.opt == '*' )
{
return this.result = val * this.num2;
} else if ( this.opt == '/' )
{
return this.result = val / this.num2;
}
},
num2:function ( val )
{
if ( this.opt == '+' )
{
this.result = this.num1 + val;
} else if ( this.opt == '-' )
{
this.result = this.num1 - val;
} else if ( this.opt == '*' )
{
this.result = this.num1 * val;
} else if ( this.opt == '/' )
{
this.result = this.num1 / val;
}
}
}
} )
</script>
</body>
</html>