使用methods属性
例子:method.html
<!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>
var app=new Vue({
el:'#app',
data:{
message:'hello Vue'
},
methods:{
greeting:function(time){
return 'good'+time+'!'
}
}
})
</script>
</body>
</html>
运行结果
使用图标集
使用语法糖
v-on→@和v-on→:
例子:点我:v-on.html
<!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">
<button type="button" @click="handleClick">点我</button>
</div>
<script type="text/javascript">
var app=new Vue({
el:'#app',
data:{
name:'软件1721'
},
methods:{
handleClick:function(){
alert(this.name);
}
}
})
</script>
</body>
</html>
运行结果:
隐藏和显示1721:v-on1
<!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>
<div id="app">
<h2 v-if="show">{{name}}</h2>
<button type="button" @click="handleClick">隐藏/显示</button>
</div>
<script>
var app=new Vue({
el:'#app',
data:{
name:'软件1721',
show:true
},
methods:{
handleClick:function(){
/* if(this.show===true){
this.show===false;
}else{
this.show===true
} */
this.show=!this.show;
}
}
})
</script>
</body></html>
运行结果:加一岁和减五岁:v-on2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<title>年龄的加减</title>
</head>
<body>
<div id="app">
<h2>{{age}}</h2>
<button type="button" @click="add">加一岁</button>
<button type="button" @click="substrct(5)">减五岁</button>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
age: 30
},
methods: {
add: function() {
/* if (this.show === true) {
this.show = false;
} else {
this.show = true;
} */
this.age += 1;
},
substrct: function(num) {
if (this.age < 5) {
alert("不能再减了");
return;
}
this.age -= num;
}
}
})
</script>
</body>
</html>
简书作者+关注:v-on3.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<title>关注和取消关注</title>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
<style type="text/css">
.followed{
color: #ddd;
}
.link{
cursor: pointer;
}
.cancle-followed{
color: green;
}
</style>
</head>
<body>
<div id="app">
<h2>{{name}}</h2>
<span class="followed link" v-show="followed" @click="handlefollow">
<i class="icon-ok"></i>已关注
</span>
<span class="cancle-followed link" v-show="followed===false" @click="handlefollow">
<i class="icon-plus"></i>关注
</span>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
name:'简述作者',
followed:false
},
methods: {
handlefollow:function(){
this.followed=!this.followed;
}
}
})
</script>
</body>
</html>
运行结果:
模仿简书下面点喜欢做的图:likestyle.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
<title>喜欢的样式</title>
<style type="text/css">
.btn{
width: 100px;
height: 40px;
color: rgb(234,111,90);
border-radius: 20px;
font-size: 14px;
background-color: #FFFFFF;
border: 1px solid rgb(234,111,90);
outline:none;
}
</style>
</head>
<body>
<div id="app">
<button type="button" @click="handlefollow" class="btn">
<span class="followed link" v-show="followed" @click="handlefollow">
<i class="icon-heart"></i>喜欢 | {{number}}
</span>
<span class="cancle-followed link" v-show="followed===false" @click="handlefollow">
<i class="icon-heart-empty"></i>喜欢 | {{number}}
</span>
</button>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
followed:false,
number: 30
},
methods: {
add: function() {
/* if (this.show === true) {
this.show = false;
} else {
this.show = true;
} */
this.number += 1;
},
substrct: function(num) {
if (this.age < 5) {
alert("不能再减了");
return;
}
this.age -= num;
}
}
})
</script>
</body>
</html>
运行结果: