Vue学习记录-10
1、学习代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../js/vue.js"></script>
<title>事件的基本使用</title>
</head>
<body>
<div id="root">
<h2>欢迎来到{{name}}学习</h2>
<button v-on:click="showInfo">点我提示信息1</button>
<!-- 简写形式 传参需要加括号-->
<button @click="showInfo2(66,$event)">点我提示信息2</button>
</div>
<script>
new Vue({
el:'#root',
data:{
name:'北方工业大学'
},
methods:{
showInfo(event){
console.log(event.target.innerText)
this.name = '北方工业大学1'
alert('同学你好')
},
showInfo2(number,a){
console.log(a,number)
alert(number)
}
}
})
</script>
</body>
</html>
2、学习总结
事件的基本使用:
- 使用v-on:或者@xxx绑定事件,其中xxx是事件名;
- 事件的回调需要配置在methods对象中,最终会在vm上;
- methods中配置的函数,不要用箭头函数,否则this就不是vm了而是window;
- methods中配置的函数,都是被Vue所管理的函数,this的指向是vm或组件实例对象;
- @click="demo"和@click="demo($event)"效果一致,但后者可以穿参数;