方法和参数
1、直接使用表达式实现
<button @click="count++">直接使用表达式实现</button>
2、绑定单个方法;没有参数调用不需要括号;响应事件可以接受一个event参数,调用时不需要传递;
methods:{
addCountClick(){
this.count++;
},
addCountClick2(event){
this.count++;
console.log(event);
console.log(event.target);
},
},
template:`
<div>调用响应事件,目前数量{
{count}}.</div>
<button @click="addCountClick">调用响应事件</button>
<button @click="addCountClick2">调用响应事件,接受event参数</button>
`
})
3、绑定单个方法,传递参数;如果要使用event参数,需要通过$event显式调用;
methods:{
addCountClickArg(num){
this.count++;
},
addCountClickArg2(num,event){
this.count++;
console.log(event);
console.log(event.target);
},
},
template:`
<div>调用响应事件-带参数,目前数量{
{count}}.</div>
<button @click="addCountClickArg(3)">调用响应事件-带参数</button>
<button @click="addCountClickArg2(5,$event)">调用响应事件-带参数,接受event参数</button>
`
})
4、一个按钮调用两个方法,通过逗号分隔,调用时无论是否有参数都需要带括号
methods:{
handleBtnClick1(){
alert(1)
},
handleBtnClick2(){
alert(2)
},
},
template:`
<button @click="handleBtnClick1(),handleBtnClick2()">一个按钮调用两个方法</button>
`
})
事件修饰符
冒泡事件:在JavaScript中有嵌套的DOM元素时,两个都有绑定事件,JS会自动向上