今天學(xué)習(xí)methods屬性, 這個(gè)名字是固定的哩牍,它是一個(gè)對(duì)象,用于存儲(chǔ)各種方法令漂。{{方法名()}}就可以調(diào)用相應(yīng)的方法膝昆。
使用method方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h2>{{greeting('afternoon')}}</h2>
<h2>{{message}}</h2>
</div>
<script type="text/javascript">
//實(shí)例化一個(gè)vue對(duì)象
var app = new Vue({
el: '#app',
data:{
message:' hello Vue',
},
methods:{
greeting:function(time){
return 'Good' + time+'!';
}
}
})
</script>
</body>
</html>
運(yùn)行結(jié)果:
image.png
使用v-on指令調(diào)用方法
- 分別點(diǎn)擊“隱藏/顯示”按鈕可以切換內(nèi)容的顯示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></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" v-on:click="handClick">隱藏/顯示</button>
</div>
<script type="text/javascript">
//實(shí)例化一個(gè)vue對(duì)象
var app = new Vue({
el: '#app',
data:{
name:'2019',
show:true
},
methods:{
handClick:function(){
// 把當(dāng)前的show屬性相反
this.show=!this.show;
}
}
})
</script>
</body>
</html>
關(guān)注/取消關(guān)注練習(xí)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></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"/>
<style type="text/css">
.follow{
color:#DDD;
}
.cancle-follow{
color:#008000;
}
.link{
cursor:pointer;
}
</style>
</head>
<body>
<!-- vue-app根容器 -->
<div id="app">
<h2>{{name}}</h2>
<p class="follow link" v-show="followed" v-on:click="handfollow">
<i class="icon-ok"></i>已關(guān)注</p>
<p class="cancle-follow link" v-show="followed===false"
v-on:click="handfollow"><i class="icon-plus"></i>關(guān)注</p>
</div>
<script type="text/javascript">
//實(shí)例化一個(gè)vue對(duì)象
var app = new Vue({
el: '#app',
data:{
name:'簡(jiǎn)書(shū)作者',
followed:false
},
methods:{
handfollow:function(followed){
this.followed=!this.followed;
}
}
})
</script>
</body>
</html>
年齡的增減練習(xí)
單擊“長(zhǎng)一歲”可以使得age的值加1,單擊“減五歲”可以使得age的值減5
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></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" v-on:click="add">加一歲</button>
<button type="button" v-on:click="substrct(5)">減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;
},
substrct:function(num){
this.age -= num;
}
}
})
</script>
</body>
</html>
大體效果:
image.png
關(guān)注和加減的綜合訓(xùn)練:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></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" />
<style type="text/css">
.btn {
color: red;
}
.link {
cursor: pointer;
}
.zan{
display:flex;
}
.btn{
width:100px;
height:35px;
border-radius: 20px;
background-color:#FFF;
border:1px solid rgb(236, 97, 73);
outline:none;
}
</style>
</head>
<body>
<!-- vue-app根容器 -->
<div id="app">
<div class="zan">
<button class="btn link" v-show="followed"
v-on:click="handfollow" v-on:click="handfollow"><i class="icon-heart"></i>喜歡 |{{age}} </button>
<button class="btn link" v-show="followed===false" v-on:click="handfollow">
<i class="icon-heart-empty"></i>喜歡 {{age}} </button>
</div>
</div>
<script type="text/javascript">
//實(shí)例化一個(gè)vue對(duì)象
var app = new Vue({
el: '#app',
data: {
age: 12,
followed: false
},
methods: {
handfollow: function(followed) {
this.followed = !this.followed;
if(this.followed==true){
this.age+=1;
}
else{
this.age-=1;
}
}
}
})
</script>
</body>
</html>
大體效果
image.png
可以點(diǎn)擊按鈕來(lái)喜歡或取消喜歡洗显,同時(shí)后面數(shù)字也會(huì)隨之改變外潜。