vue 的属性绑定

双大括号不能在 HTML attributes 中使用。想要响应式地绑定一个 attribute,应该使用 v-bind 指令。

<template>
<div v-bind:class="boxClass" v-bind:id="boxId">
</div>
</template>

<script>
export default{
  data(){
    return{
      boxClass:"box1",
      boxId: "boxid1"
    }
  }
}
</script>

 

注意:v-bind 指令指示 Vue 将元素的 id attribute 与组件的 dynamicid 属性保持一致。如果绑定的值是 null 或者undefined ,那么该 attribute 将会从渲染的元素上移除 。

由于v-bind比较常用 所以可以简写,将v-bind省略只留下冒号。

<div :class="boxClass" :id="boxId"></div>

布尔型 

布尔型 attribute 依据 true /false 值来决定 attribute 是否应该存在于该元素上,disabled 就是最常见的例子之一。

<template>
<div :class="boxClass" :id="boxId">
  <button :disabled="flag"></button>
</div>
</template>

<script>
export default{
  data(){
    return{
      boxClass:"box1",
      boxId: "boxid1",
      flag:true,
    }
  }
}
</script>

动态绑定多个值 

如果需要绑定多个值可以使用js的对象来实现

<template>
<div v-bind="object"></div>
</template>

<script>
export default{
  data(){
    return{
      object:{
        boxClass:"box1",
        boxId:"boxid1"
      }
    }
  }
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值