03 属性绑定

目录

属性绑定

简写

布尔型 Attribute

动态绑定多个值

属性绑定

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

 <template>
   <div v-bind:id="dynamicId" v-bind:class="dynamicClass">AppID</div>
 <template>
 <script>
 export default {
   data() {
      return {
        dynamicId: "appId",
        dynamicClass: "appClass",
     } 
   }
 }
 </script>
 <style>
 .appclass {
   color: red;
   font-size: 20px;
 }
 </style>

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

简写

因为v-bind非常常用,有特定的简写语法

 <div :id="dynamicId" :class="dynamicClass"></div>

布尔型 Attribute

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

 <template>
   <button :disabled="isButtonDisabled">Button</button>
 <template>
 <script>
 export default {
   data() {
      return {
        isButtonDisabled: true
     } 
   }
 }
 </script>

动态绑定多个值

如果你有像这样的一个包含多个 attribute 的 JavaScript 对象

<template>
   <div v-bind="objectOfAttrs">百战程序员</div>
 <template>
 <script>
 export default {
   data() {
      return {
        objectOfAttrs: {
          id: 'container',
          class: 'wrapper'
       }
     } 
   }
 }
 </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值