1.1、绑定class属性,由绑定的变量来决定应用哪个样式
1.2、可以将样式直接设置成Vue组件中的数据对象
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<style>
.blue{
color: blue;
}
.red{
color: red;
}
.green{
color: green;
}
.font{
font-size: 30px;
}
style>
<body>
<div id="app">
<div v-bind:class="{blue:isblue,red:isred}">
西安
div>
<div v-bind:class="[greenClass,fontClass]">
郑州
div>
<div :class="style">
武汉
div>
div>
<script>
const obj={
data(){
return{
style:{
blue:true,
red:false
},
isblue:true,
isred:false,
greenClass:'green',
fontClass:'font',
textColor:'red',
textFont:'35px'
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
注意:v-bind可以缩写为:
<div :style=""></div>
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<style>
.blue{
color: blue;
}
.red{
color: red;
}
.green{
color: green;
}
.font{
font-size: 30px;
}
style>
<body>
<div id="app">
<div :style="{color:textColor,fontSize:textFont}">
济南
div>
div>
<script>
const obj={
data(){
return{
textColor:'red',
textFont:'35px'
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
注意:
内联的CSS样式属性名与外部的CSS样式属性名有区别的。
内联的CSS样式属性名:通常采用驼峰命名.例如:fontSize
外部的CSS样式属性名:采用’-'进行连接.例如: font-size
2.1、获取事件类型:$event
2.2、多事件处理的语法格式:@事件名 = “方法1,方法2”
演示:
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="app">
<div>点击次数 {{ count }}div>
<button @click="myclick(2),logs($event)">点击button>
div>
<script>
const obj={
data(){
return{
count:0
}
},
methods:{
myclick(step){
this.count+= step
},
logs(event){
console.log(event)
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
3.1、DOM的事件原理:
首先会从父组件开始依次传递到子组件 — 事件捕获
其次当事件传递到最内层的子组件时,会逆向再进行一次传递,从子组件依次向上传递 — 事件冒泡
3.2、Vue中使用v-on指令绑定事件时默认监听的是DOM事件的冒泡阶段
演示:
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="app">
<div id="d1" @click="click1" style="border: solid red 1px;">
外层
<div id="d2" @click="click2" style="border: solid blue 1px;">
中间
<div id="d3" @click="click3" style="border: solid green 1px;">
内层
div>
div>
div>
div>
<script>
const obj={
methods:{
click1(){
console.log('外层')
},
click2(){
console.log('中间')
},
click3(){
console.log('内层')
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
在点击内层后,输出为冒泡阶段,即内层->外层—>外层
3.3、如果需要监听捕获阶段的事件,在Vue中使用capture事件修饰符来实现
演示:
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="app">
<div id="d1" @click.capture="click1" style="border: solid red 1px;">
外层
<div id="d2" @click.capture="click2" style="border: solid blue 1px;">
中间
<div id="d3" @click.capture="click3" style="border: solid green 1px;">
内层
div>
div>
div>
div>
<script>
const obj={
methods:{
click1(){
console.log('外层')
},
click2(){
console.log('中间')
},
click3(){
console.log('内层')
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
在点击内层后,输出为事件捕获阶段,即外层->中层->内层
3.4、阻止事件的传递:stop
演示:
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="app">
<div id="d1" @click.stop="click1" style="border: solid red 1px;">
外层
<div id="d2" @click.stop="click2" style="border: solid blue 1px;">
中间
<div id="d3" @click.stop="click3" style="border: solid green 1px;">
内层
div>
div>
div>
div>
<script>
const obj={
methods:{
click1(){
console.log('外层')
},
click2(){
console.log('中间')
},
click3(){
console.log('内层')
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
3.5、只触发一次事件:once
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>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="app">
<div id="d1" @click.once="click1" style="border: solid red 1px;">
外层
<div id="d2" @click.once="click2" style="border: solid blue 1px;">
中间
<div id="d3" @click.once="click3" style="border: solid green 1px;">
内层
div>
div>
div>
div>
<script>
const obj={
methods:{
click1(){
console.log('外层')
},
click2(){
console.log('中间')
},
click3(){
console.log('内层')
}
}
}
Vue.createApp(obj).mount('#app')
script>
body>
html>
3.6、禁止默认事件:prevent
<!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>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app">
<a href="http://www.baidu.com" @click.prevent="myclick">百度</a>
</div>
<script>
const obj={
data(){
methods:{
myclick:{
console.log('点击成功')
}
}
}
}
Vue.createApp('obj').mount('#app')
</script>
</body>
</html>
点击后不会跳转