v-on的参数问题
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 事件调用的方法没有参数 -->
<button @click="btnClick">按钮1</button>
<button @click="btnClick()">按钮2</button>
<!-- 在有参数的方法中,如果不带括号 -->
<button @click="btnClick1">按钮3</button>
<button @click="btnClick1()">按钮4</button>
<button @click="btnClick1(123)">按钮5</button>
<button @click="btnClick1('abc')">按钮6</button>
<button @click="btnClick2('abc')">按钮7</button>
<button @click="btnClick2('abc', $event)">按钮8</button>
</div>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
content: "你好呀"
},
methods: {
btnClick: function(){
alert(this.content);
},
btnClick1: function(abc){
alert(abc);
},
btnClick2(a, b){
console.log("++++++"+a+b)
}
},
})
</script>
</body>
</html>
v-on的修饰符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div @click="divClick">
<button @click="btnClick">按钮1.1</button>
<!-- .stop -->
<button @click.stop="btnClick">按钮1.2</button>
</div>
<form action="https://www.baidu.com/">
<!--点击完后跳转-->
<input type="submit" value="提交2.1" @click="submitClick">
<!-- .prevent 点击完后不跳转-->
<input type="submit" value="提交2.2" @click.prevent="submitClick">
</form>
<input type="text" @keyup="keyUp">
<br>
<!-- 监听某个键 -->
<input type="text" @keyup.a="keyUp">
<br>
<!-- .once只有第一次点击有用 -->
<button @click.once="btnClick1">按钮</button>
</div>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
message: "sb"
},
methods: {
divClick: function(){
alert("div");
},
btnClick: function(){
alert("button");
},
submitClick: function(){
alert("submit");
},
keyUp: function(){
alert("keyWord");
},
btnClick1(){
alert("出来了");
}
},
})
</script>
</body>
</html>
v-on基本使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-on方法处理器</title>
</head>
<body>
<div id="con">
<h2>当前计数:{{counter}}</h2>
<!-- <button v-on:click="counter--">-</button>
<button v-on:click="counter++">+</button> -->
<!-- <button v-on:click="sub">-</button>
<button v-on:click="add">+</button> -->
<!-- 语法糖 -->
<button @click="sub">-</button>
<button @click="add">+</button>
</div>
</body>
<script src="../vue.js"></script>
<script>
const app = new Vue({
el: "#con",
data: {
counter:0
},
methods: {
add: function(){
// app.counter++
this.counter++
},
sub: function(){
// app.counter--
this.counter--
}
}
});
</script>
</html>