目录


前言:


一、 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>
  <div id="app">
    <h2 class="title" :class="{active:isActive}">{{message}}</h2>
    <h2 class="title" :class="getClasses()">{{message}}</h2>
    <button @click="change">点击变色</button>
  <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">
    <!-- 加上单引号当成字符串 -->
    <h2 class="title" :class="['active','line']">{{message}}</h2>
    <!-- 不加会被当成变量 -->
    <h2 class="title" :class="[active,line]">{{message}}</h2>
    <h2 class="title" :class="getClasses()">{{message} {{getClasses()[1]}}</h2>
  <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(对象语法)

<!-- <h2 :style="{key(属性名):value(属性值)}">{{message}}</h2> -->
<!-- 加单引号,当成字符串解析 -->
<h2 :style="{fontSize:'50px'}">{{message}}</h2>
<!-- 不加单引号,变量解析 -->
<h2 :style="{fontSize:fontSize}">{{message}}</h2>
<h2 :style="getClasses()">{{message}}</h2>





    
  <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">
    <h2 :style="[baseStyle]">{{message}}</h2>
    <h2 :style="getStyle()">{{message}}</h2>
  <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>
    <div id="app">
            <li :class="currentIndex==index?'active':''" @click="change(index)" v-for="(item,index) in list">{{index}}--{{item}}</li>
    <script src="./vue.js"></script>
    <script>
        let vm = new Vue({
            el:'#app',
            data(){
                return {
                    currentIndex:0,
                    list:["小明","小红","小张","小李"],
            methods:{
                change(index){
                    this.currentIndex=index
    </script>