双大括号不能在 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>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |