1、v-on起步練習(xí)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>V-on起步</title>
<!-- 通過(guò)CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- Vue-app的根容器 -->
<div id="app">
<button type="button" @click="handleClick">點(diǎn)我</button>
</div>
<script type="text/javascript">
// 實(shí)例化一個(gè)Vue對(duì)象
var app=new Vue({
el:'#app',
data:{
name: '軟件1721',
},
methods: {
handleClick:function(){
alert(this.name);
}
}
})
</script>
</body>
</html>
運(yùn)行結(jié)果:

image
2、V-on練習(xí)2-隱藏和顯示的切換練習(xí)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>V-on練習(xí)2-隱藏和顯示的切換練習(xí)</title>
<!-- 通過(guò)CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- Vue-app的根容器 -->
<div id="app">
<h2 v-if="show">{{name}}</h2>
<button type="button" @click="handleClick">隱藏/顯示</button>
</div>
<script type="text/javascript">
// 實(shí)例化一個(gè)Vue對(duì)象
var app=new Vue({
el:'#app',
data:{
show:true,
name: '軟件1721',
},
methods: {
handleClick:function(){
// 把當(dāng)前show屬性的值取反
/* if (this.show === true) {
this.show=false;
} else{
this.show=true;
} */
this.show=!this.show
}
}
})
</script>
</body>
</html>
運(yùn)行結(jié)果:

image
3、V-on練習(xí)3-年齡的加減
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>V-on練習(xí)3-年齡的加減</title>
<!-- 通過(guò)CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!-- Vue-app的根容器 -->
<div id="app">
<h2> {{age}}</h2>
<button type="button" @click="add">加一歲</button>
<button type="button" @click="substract(5)">減五歲</button>
</div>
<script type="text/javascript">
// 實(shí)例化一個(gè)Vue對(duì)象
var app=new Vue({
el:'#app',
data:{
age:30
},
methods: {
add: function(){
this.age += 1;
},
substract: function(num){
// 判斷當(dāng)前年齡是否夠減
if (this.age -num<0) {
alert('不夠減了')
} else{
this.age -= num;
}
}
}
})
</script>
</body>
</html>
運(yùn)行結(jié)果:

image
4、V-on練習(xí)4-結(jié)合圖標(biāo)關(guān)注和取關(guān)的練習(xí)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>V-on練習(xí)4-結(jié)合圖標(biāo)關(guān)注和取關(guān)的練習(xí)</title>
<!-- 通過(guò)CDN引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
</head>
<style type="text/css">
.followed{
color: darkgrey;
}
.link{
cursor: pointer;
}
.cancle-followed{
color: #008000;
}
</style>
<body>
<!-- Vue-app的根容器 -->
<div id="app">
<h2>{{name}}</h2>
<span class="followed link" v-show="followed" @click="handleFollow">
<i class ="icon-ok"></i>已關(guān)注
</span>
<span class="cancle-followed link" v-show="followed === false" @click="handleFollow">
<i class ="icon-plus"></i>關(guān)注
</span>
</div>
<script type="text/javascript">
// 實(shí)例化一個(gè)Vue對(duì)象
var app=new Vue({
el:'#app',
data: {
name: '簡(jiǎn)書(shū)作者',
followed:false
},
methods: {
handleFollow: function(){
this.followed=!this.followed;
}
}
})
</script>
</body>
</html>
運(yùn)行結(jié)果:

image
- 總結(jié):methods屬性, 這個(gè)名字是固定的,它是一個(gè)對(duì)象,用于存儲(chǔ)各種方法。{{方法名()}}就可以調(diào)用相應(yīng)的方法