vue 的属性绑定

[复制链接]
发表于 2026-1-12 17:49:38 | 显示全部楼层 |阅读模式
双大括号不能在 HTML attributes 中利用。想要相应式地绑定一个 attribute,应该利用 v-bind 指令。
  1. <template>
  2. <div v-bind:class="boxClass" v-bind:id="boxId">
  3. </div>
  4. </template>
  5. <script>
  6. export default{
  7.   data(){
  8.     return{
  9.       boxClass:"box1",
  10.       boxId: "boxid1"
  11.     }
  12.   }
  13. }
  14. </script>
复制代码
 

留意:v-bind 指令指示 Vue 将元素的 id attribute 与组件的 dynamicid 属性保持划一。如果绑定的值是 null 大概undefined ,那么该 attribute 将会从渲染的元素上移除 。
由于v-bind比力常用 以是可以简写,将v-bind省略只留下冒号。
  1. <div :class="boxClass" :id="boxId"></div>
复制代码
布尔型 

布尔型 attribute 依据 true /false 值来决定 attribute 是否应该存在于该元素上,disabled 就是最常见的例子之一。
  1. <template>
  2. <div :class="boxClass" :id="boxId">
  3.   <button :disabled="flag"></button>
  4. </div>
  5. </template>
  6. <script>
  7. export default{
  8.   data(){
  9.     return{
  10.       boxClass:"box1",
  11.       boxId: "boxid1",
  12.       flag:true,
  13.     }
  14.   }
  15. }
  16. </script>
复制代码

动态绑定多个值 

如果必要绑定多个值可以利用js的对象来实现
  1. <template>
  2. <div v-bind="object"></div>
  3. </template>
  4. <script>
  5. export default{
  6.   data(){
  7.     return{
  8.       object:{
  9.         boxClass:"box1",
  10.         boxId:"boxid1"
  11.       }
  12.     }
  13.   }
  14. }
  15. </script>
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表