methods属性,它是一个对象,用于存储各种方法。{{方法名()}}就可以调用相应的方法。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>v-on起步练习</title>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<!--vue-app根容器-->
<div id="app">
<button type="button" v-on:dblclick="handleClick">点我</button>
</div>
<script type="text/javascript">
// 实例化Vue对象
var app = new Vue({
el: '#app',
data: {
name: '软件1721',
},
methods:{
handleClick:function(){
alert(this.name);
}
}
})
</script>
</body>
</html>
运行结果.png
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>v-on起步练习2--隐藏和显示的切换练习</title>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<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:dblclick="handleClick">点我</button>
</div>
<script type="text/javascript">
// 实例化Vue对象
var app = new Vue({
el: '#app',
data: {
name: '软件1721',
show:true
},
methods:{
handleClick:function(){
//把当前show属性的值取反
//if(this.show === true){
// this.show =false;
//}else{
// this.show = true;
//}
this.show=!this.show;
}
}
})
</script>
</body>
</html>
运行结果.png
运行结果png
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>v-on起步练习3--年龄的加减</title>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<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="substrct(5)">减五岁</button>
</div>
<script type="text/javascript">
// 实例化Vue对象
var app = new Vue({
el: '#app',
data: {
age:30
},
methods:{
add:function(){
this.age += 1;
},
substrct:function(num){
this.age -= num;
}
}
})
</script>
运行结果.png
运行结果.png
运行结果.png
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>v-on指令练习5-喜欢的练习</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" href="/vue-basic/css/font-awesome.min.css">
<style type="text/css">
.like {
width: 100px;
height: 50px;
color: rgb(234, 111, 90);
border-radius: 25px;
font-size: 14px;
background-color: #FFF;
border: 1px solid rgb(234, 111, 90);
outline: none;
}
.dislike {
width: 100px;
height: 50px;
color: #FFF;
border-radius: 25px;
font-size: 14px;
background-color: rgb(234, 111, 90);
border: 1px solid rgb(234, 111, 90);
/* 点击取消外边框 */
outline: none;
}
</style>
</head>
<body>
<div id="app">
<button class="dislike" v-show="status == false" @click="like">
<i class="icon-heart"></i> 喜欢 | {{number}}
</button>
<button class="like" v-show="status" @click="dislike">
<i class="icon-heart-empty"></i> 不喜欢 | {{number}}
</button>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
status: true,
number: 1,
},
methods: {
like: function() {
this.status = !this.status;
this.number -= 1;
},
dislike: function() {
this.status = !this.status;
this.number += 1;
}
}
})
</script>
</body>
</html>
运行结果.png
运行结果.png
网友评论