目录 前言: 一、v-bind动态绑定class 1. v-bind动态绑定class(对象语法) 2. v-bind动态绑定class(数组用法) 3.v-bind动态绑定style(对象语法) 4.v-bind动态绑定style(数组语法) 二、v-bind和v-for的结合使用
目录
- 前言:
- 一、 v-bind动态绑定class
- 1. v-bind动态绑定class(对象语法)
- 2. v-bind动态绑定class(数组用法)
- 3.v-bind动态绑定style(对象语法)
- 4.v-bind动态绑定style(数组语法)
- 二、v-bind和v-for的结合使用
前言:
在昨天的文章中已经基本介绍了,v-bind的基本使用,可以参考学习,本文是更加具体的解释v-bind的使用,和v-for结合的使用。
一、 v-bind动态绑定class
1. v-bind动态绑定class(对象语法)
这里举简单例子来体现绑定效果,设置一个按钮可以控制对象里面元素的颜色,这时候对象就要绑定动态事件,才能实现变化。
有两种绑定的方法
一种直接在h5中直接绑定颜色变化,另一种是调用函数的方式绑定,需要注意,直接写就不需要this,但是在调用函数里面需要使用this才能实现。其中还要注意,变色的条件是自己设置了一个为false点击后取反出现,然后再取反,就会出现变色不变色的样式。
代码如下:
<style>
.active{
color:red;
}
</style>
</head>
<body>
<div id="app">
<h3 class="title" :class="{active:isActive}">{{message}}</h3>
<h3 class="title" :class="getClasses()">{{message}}</h3>
<button @click="change">点击变色</button>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
message:"你好啊",
active:"active",
isActive:false
},
methods: {
change(){
this.isActive = !this.isActive
},
getClasses(){
return {active:this.isActive}
}
},
})
</script>
2. v-bind动态绑定class(数组用法)
class属性中可以放数组,会依次解析成对应的class。
- 加上单引号的表示字符串
- 不加的会当成变量
- 可以直接使用方法返回数组对象
<div id="app">
<!-- 加上单引号当成字符串 -->
<h3 class="title" :class="['active','line']">{{message}}</h3>
<!-- 不加会被当成变量 -->
<h3 class="title" :class="[active,line]">{{message}}</h3>
<h3 class="title" :class="getClasses()">{{message} {{getClasses()[1]}}</h3>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
message:"你好啊",
active:"aaaa",
line:'bbbb'
},
methods: {
getClasses(){
return [this.active,this.line]
}
},
})
</script>
3.v-bind动态绑定style(对象语法)
<!-- <h3 :style="{key(属性名):value(属性值)}">{{message}}</h3> -->
<!-- 加单引号,当成字符串解析 -->
<h3 :style="{fontSize:'50px'}">{{message}}</h3>
<!-- 不加单引号,变量解析 -->
<h3 :style="{fontSize:fontSize}">{{message}}</h3>
<h3 :style="getClasses()">{{message}}</h3>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data(){
return{ message:"你好啊",
fontSize:50+'px'
}
},
methods: {
getClasses(){
return {fontSize:this.fontSize}
}
},
})
</script>
4.v-bind动态绑定style(数组语法)
<div id="app">
<h3 :style="[baseStyle]">{{message}}</h3>
<h3 :style="getStyle()">{{message}}</h3>
</div>
<script src="vue.js"></script>
<script>
const app = new Vue({
el:"#app",
data:{
message:"你好啊",
baseStyle:{backgroundColor:'red'}
},
methods: {
getStyle(){
return [this.baseStyle]
}
},
})
</script>
二、v-bind和v-for的结合使用
实现了,点击字体会出现变色,前提是需要先通过v-for把数组内的内容全部都遍历出来,在通过绑定事件,来实现颜色的切换,其中需要注意,自己设置了一个当前的索引,通这个索引来判断当前的情况,从而发生颜色的变化,实现所需的结果。
<style>
.active{
color: red;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li :class="currentIndex==index?'active':''" @click="change(index)" v-for="(item,index) in list">{{index}}--{{item}}</li>
</ul>
</div>
<script src="./vue.js"></script>
<script>
let vm = new Vue({
el:'#app',
data(){
return {
currentIndex:0,
list:["小明","小红","小张","小李"],
}
},
methods:{
change(index){
this.currentIndex=index
}
}
})
</script>
以上就是Vue 中指令v-bind动态绑定及与v-for结合使用详解的详细内容,更多关于Vue 指令v-bind结合v-for的资料请关注易盾网络其它相关文章!
