vue 绑定 HTML Class

news/2024/7/10 3:15:17 标签: vue

绑定 HTML Class

我们可以传给 v-bind:class 一个对象,以动态地切换 class:

<div v-bind:class="{ active: isActive }"></div>

上面的语法表示 active 这个 class 存在与否将取决于数据属性 isActive 的 true还是false
你可以在对象中传入更多属性来动态切换多个 class。此外,v-bind:class 指令也可以与普通的 class 属性共存。当有如下模板:

<div
  class="static"
  v-bind:class="{ active: isActive, 'text-danger': hasError }"
></div>
data: {
  isActive: true,
  hasError: false
}

结果渲染为:

<div class="static active"></div>

绑定的数据对象不必内联定义在模板里:

<div v-bind:class="classObject"></div>
data: {
  classObject: {
    active: true,
    'text-danger': false
  }
}

渲染的结果和上面一样。

我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:

<div v-bind:class="classObject"></div>
data: {
  isActive: true,
  error: null
},
computed: {
  classObject: function () {
    return {
      active: this.isActive && !this.error,
      'text-danger': this.error && this.error.type === 'fatal'
    }
  }
}

渲染的结果和上面一样。


http://www.niftyadmin.cn/n/1301874.html

相关文章

Vue2.0 中vuex 的简单使用

首先安装vuex插件到项目中 cnpm install vuex --save 在项目的src目录下面建一个store的文件夹&#xff0c;并建一个index.js的文件 //引入vuex import Vue from vue; import Vuex from vuex;Vue.use(Vuex); 在index.js申明一个状态值count,并声明两个方法来改变该状态值&a…

JS算法基础、进阶

一、JS算法的基础 1、数组去重 function qc(arr1){ //创建一个新的数组let arr []; //遍历数组arr1for( let i 0; i < arr1.length; i) { //如果arr1不在arr中 会返回-1 那么将和这个元素存在新建的arr中if( arr.indexOf(arr1[i]) -1) {//indexOf判断arr1是否在arr数组…

修改nginx限制上传文件的大小

先找到linux下的**/usr/local/nginx/conf**&#xff0c;nginx.conf 加上 client_max_body_size 10m; 这句话 http {include /etc/nginx/mime.types;default_type application/octet-stream;charset utf-8;log_format main $remote_addr - $remote_user [$time_local]…

SCSS语法

嵌套规则 // scss #id {color: red;.name {color: blue;.child {color: yellow;}} }// css #id {color: red; }#id .name {color: blue; }#id .name .child {color: yellow; }引用父选择器 & // scss .btn {background-color: #fff;&.active {background-color: red;…

tp3 除了首页,其他页面报错解决方案(nginx方案)

除了首页能访问&#xff0c;其他路由跳转都是Not Found&#xff0c;然后在域名后面加index.php就能访问&#xff0c;比如http://localhost:81/index.php/admin/&#xff0c;这样就能访问&#xff0c;去掉index.php就不行。 解决方法&#xff1a; 配置中隐藏掉index.php即可 打开…

一般前端登录表单提交校验方法

一般前端登录表单提交校验方法 html <form action"" method"post"><div class"inputItem flex"><p>手机号码&#xff1a;</p><input type"tel" placeholder"请输入手机号码" id"userPhon…

ssh连接的时候出现Host key verification failed.

今天在服务器上执行远程操作命令出现以下的问题: [rootwww ~]# ssh 205.209.161.** WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middl…

在APACHE服务器上的访问方式上去除index.php(伪静态)

在APACHE服务器上的访问方式上去除index.php 下面我说下 apache 下 &#xff0c;如何 去掉URL 里面的 index.php 例如: 你原来的路径是&#xff1a; localhost/index.php/index 改变后的路径是: localhost/index 1.httpd.conf配置文件中 #LoadModule rewrite_module modules/…