目录
事件修饰符
Vue中的事件修饰符:
1.prevent:阻止默认事件(常用)
2.stop:阻止事件冒泡(常用)
3.once:事件只触发一次(常用)
4.capture:使用事件的捕获模式
5.self:只有event.target是当前操作的元素是才触发事件
6.passive:事件的默认行为立即执行,无需等待事件回调执行完毕
代码实现
<!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">
<title>事件修饰符</title>
<!-- 引入Vue -->
<script type="text/javascript"src="../js/vue.js"></script>
<style>
*{
margin-top: 20px;
}
.demo1{
height: 50px;
background-color: skyblue;
}
.box1{
padding: 5px;
background-color: skyblue;
}
.box2{
padding: 5px;
background-color: orange;
}
.list{
width: 200px;
height: 200px;
background-color: peru;
overflow: auto;
}
li{
height: 100px;
}
</style>
</head>
<body>
<!--
Vue中的事件修饰符:
1.prevent:阻止默认事件(常用)
2.stop:阻止事件冒泡(常用)
3.once:事件只触发一次(常用)
4.capture:使用事件的捕获模式
5.self:只有event.target是当前操作的元素是才触发事件
6.passive:事件的默认行为立即执行,无需等待事件回调执行完毕
-->
<!-- 准备好一个容器 -->
<div id="root">
<h2>欢迎来到{{name}}学习</h2>
<!-- 阻止默认事件(常用) -->
<a href="http://www/atguigu.com"@click.prevent="showInfo">点我提示信息</a>
<!--阻止事件冒泡(常用) -->
<div class="demo1"@click="showInfo">
<button @click.stop="showInfo">点我提示信息</button>
</div>
<!--事件只触发一次(常用) -->
<button @click.once="showInfo">点我提示信息</button>
<!--使用事件的捕获模式 -->
<div class="box1"@click.capture="showMsg(1)">
div1
<div class="box2"@click="showMsg(2)">
div2
</div>
</div>
<!-- 只有event.target是当前操作的元素是才触发事件 -->
<div class="demo1"@click.self="showInfo">
<button @click="showInfo">点我提示信息</button>
</div>
<!-- passive:事件的默认行为立即执行,无需等待事件回调执行完毕 -->
<ul @wheel.passive="demo" class="list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip=false//阻止vue在启动时生成生产提示
new Vue({
el:'#root',
data:{
name:'尚硅谷'
},
methods:{
showInfo(e){
e.stopPropagation()
alert('同学你好!')
},
showMsg(msg){
console.log(msg)
},
demo(){
for(let i=0;i<100000;i++){
console.log("#")
}
console.log('累坏了')
}
}
})
</script>
</html>
运行效果
键盘事件
1.Vue中常用的按键别名:
回车=>enter
删除=>delete(捕获"删除"和"退格"键)
退出=>esc
空格=>space
换行=>tab
上=>up
下=>down
左=>left
右=>right
2.Vue未提供别名的按键,可以使用按键原始的key值去绑定,但注意要转为kebab-case(短横线命名)
3.系统修饰键(用法特殊):ctrl、alt、alt、shift、meta
(1)配合keyup使用:按下修饰键的同时,再按下其他键,随后释放其他键。事件才被触发
(2)配合keydown使用,正常触发事件
4.也可以使用keyCode去指定具体的按键(不推荐)
5.Vue.config.keyCodes.自定义键名=键码,可以去定制按键别名
代码实现
<!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">
<title>键盘事件</title>
<!-- 引入Vue -->
<script type="text/javascript"src="../js/vue.js"></script>
</head>
<body>
<!--
1.Vue中常用的按键别名:
回车=>enter
删除=>delete(捕获"删除"和"退格"键)
退出=>esc
空格=>space
换行=>tab
上=>up
下=>down
左=>left
右=>right
2.Vue未提供别名的按键,可以使用按键原始的key值去绑定,但注意要转为kebab-case(短横线命名)
3.系统修饰键(用法特殊):ctrl、alt、alt、shift、meta
(1)配合keyup使用:按下修饰键的同时,再按下其他键,随后释放其他键。事件才被触发
(2)配合keydown使用,正常触发事件
4.也可以使用keyCode去指定具体的按键(不推荐)
5.Vue.config.keyCodes.自定义键名=键码,可以去定制按键别名
-->
<!-- 准备好一个容器 -->
<div id="root">
<h2>欢迎来到{{name}}学习</h2>
<input type="text"placeolder="按下回车提示输入"@keydown.enter="showInfo">
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip=false//阻止vue在启动时生成生产提示
Vue.config.productionTip=13//定义了一个别名
new Vue({
el:'#root',
data:{
name:'尚硅谷'
},
methods:{
showInfo(e){
// console.log(e.key,e.keyCode)
console.log(e.target.value)
}
},
})
</script>
</html>
运行结果
姓名案例
插值语法实现
代码实现
<!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">
<title>姓名案例_插值语法实现</title>
<!-- 引入Vue -->
<script type="text/javascript"src="../js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text"v-model="firstName"><br/><br/>
名:<input type="text"v-model="lastName"><br/><br/>
姓名:<span>{{firstName.slice(0,3)}}-{{lastName}}</span>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip=false//阻止vue在启动时生成生产提示
new Vue({
el:'#root',
data:{
firstName:'张',
lastName:'三'
}
})
</script>
</html>
运行结果
methods实现
代码实现
<!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">
<title>姓名案例_methods实现</title>
<!-- 引入Vue -->
<script type="text/javascript"src="../js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text"v-model="firstName"><br/><br/>
名:<input type="text"v-model="lastName"><br/><br/>
姓名:<span>{{fullName()}}</span>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip=false//阻止vue在启动时生成生产提示
new Vue({
el:'#root',
data:{
firstName:'张',
lastName:'三'
},
methods:{
fullName(){
return this.firstName+'-'+this.lastName
}
},
})
</script>
</html>
运行结果
计算属性
计算属性
1.定义:要用的属性不存在,要通过已有属性计算得来
2.原理:底层借助了Object.defineproperty方法提供的getter和setter
3.get函数什么时候执行
(1)初次读取时会执行一次
(2)当依赖的数据发生改变时会被再次调用
4.优势:与methods实现相比,内部有缓存机制(复用),效率更高
5.备注:
1.计算属性最终会出现在vm上,直接读取使用即可
2.如果计算属性要被修改,那必须写set函数去相应修改,且set中要引起计算时依赖的数据发生改变
代码实现
<!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">
<title>姓名案例_计算属性实现</title>
<!-- 引入Vue -->
<script type="text/javascript"src="../js/vue.js"></script>
</head>
<body>
<!--
计算属性
1.定义:要用的属性不存在,要通过已有属性计算得来
2.原理:底层借助了Object.defineproperty方法提供的getter和setter
3.get函数什么时候执行
(1)初次读取时会执行一次
(2)当依赖的数据发生改变时会被再次调用
4.优势:与methods实现相比,内部有缓存机制(复用),效率更高
5.备注:
1.计算属性最终会出现在vm上,直接读取使用即可
2.如果计算属性要被修改,那必须写set函数去相应修改,且set中要引起计算时依赖的数据发生改变
-->
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text"v-model="firstName"><br/><br/>
名:<input type="text"v-model="lastName"><br/><br/>
姓名:<span>{{fullName}}</span><br/><br/>
姓名:<span>{{fullName}}</span><br/><br/>
姓名:<span>{{fullName}}</span><br/><br/>
姓名:<span>{{fullName}}</span>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip=false//阻止vue在启动时生成生产提示
const vm=new Vue({
el:'#root',
data:{
firstName:'张',
lastName:'三',
x:'你好'
},
methods:{
demo(){
}
},
computed:{
fullName:{
//get有什么作用?当有人读取fullName时,get就会被调用,且返回值就作为fullName的值
//get什么时候调用?1.初次读取fullName时 2.所依赖的数据发生变化时
get(){
console.log('get被调用了')
console.log(this)
return this.firstName+'-'+this.lastName
},
//get什么时候调用?当fullName被修改时
set(value){
console.log('set',value)
const arr=value.split('-')
this.firstName=arr[0]
this.lastName=arr[1]
}
}
}
})
</script>
</html>
运行效果
计算属性_简写
代码实现
<!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">
<title>姓名案例_计算属性实现</title>
<!-- 引入Vue -->
<script type="text/javascript"src="../js/vue.js"></script>
</head>
<body>
<!-- 准备好一个容器 -->
<div id="root">
姓:<input type="text"v-model="firstName"><br/><br/>
名:<input type="text"v-model="lastName"><br/><br/>
姓名:<span>{{fullName}}</span><br/><br/>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip=false//阻止vue在启动时生成生产提示
const vm=new Vue({
el:'#root',
data:{
firstName:'张',
lastName:'三',
},
computed:{
/* fullName:{
get(){
console.log('get被调用了')
console.log(this)
return this.firstName+'-'+this.lastName
},
}*/
//简写
fullName:function(){
console.log('get被调用了')
return this.firstName+'-'+this.lastName
}
}
})
</script>
</html>
运行结果