『VUE』04. 模板语法-属性绑定(详细图文注释)

本文详细介绍了Vue中v-bind属性的使用,包括省略写法、处理null和undefined值、逻辑值绑定(如disabled属性)以及动态绑定的应用实例,帮助读者更好地理解和实践Vue的模板绑定机制。
摘要由CSDN通过智能技术生成


欢迎关注 『VUE』 专栏,持续更新中
欢迎关注 『VUE』 专栏,持续更新中

v-bind 属性绑定

Mustache 语法不能在 HTML 属性中使用,然而,可以使用 v-bind指令

<template>
  <div class="{{staus}}">div测试</div>
</template>

<script>
export default {
  data() {
    return {
      staus: "active",
    };
  },
};
</script>

在这里插入图片描述

<template>
  <div v-bind:class="staus">div测试</div>
</template>

<script>
export default {
  data() {
    return {
      staus: "active",
    };
  },
};
</script>

在这里插入图片描述


省略写法

在vue中v-bind可以直接省略不写,效果一样

<template>
  <div :class="no_use">省略写法</div>
  <br />
  <div v-bind:title="no_use">div</div>
</template>

<script>
export default {
  data() {
    return {
      no_use: "191",
    };
  },
};
</script>


如果绑定的值是null或underfined 属性省略

但是我试了一下,class不论是不是null都会显示,但是div的title会省略掉

<template>
  <div :class="no_use">省略写法</div>
  <br />
  <div v-bind:title="no_use1">div1</div>
  <br />
  <div v-bind:title="no_use2">div2</div>
  <br />
  <div v-bind:title="no_use3">div3</div>
</template>

<script>
export default {
  data() {
    return {
      staus: "active",
      no_use1: "191",
      no_use2: null,
      no_use3: undefined,
    };
  },
};
</script>

在这里插入图片描述
如果是class属性的话

<template>
  <div :class="no_use">省略写法</div>
  <br />
  <div v-bind:class="no_use1">div1</div>
  <br />
  <div v-bind:class="no_use2">div2</div>
  <br />
  <div v-bind:class="no_use3">div3</div>
</template>

<script>
export default {
  data() {
    return {
      staus: "active",
      no_use1: "191",
      no_use2: null,
      no_use3: undefined,
    };
  },
};
</script>

在这里插入代码片


逻辑值绑定

disabled表示无法点击

<template>
  <div v-bind:class="staus">div测试</div>
  <button v-bind:disabled="isButtonDisabled">按钮</button>
</template>

<script>
export default {
  data() {
    return {
      staus: "active",
      isButtonDisabled: false,
    };
  },
};
</script>

<style>
.active {
  color: red;
  font-size: larger;
}
</style>

在这里插入图片描述


动态绑定

在js中新建一个类似对象的东西,然后v-bind绑定对象,后续比如说按钮的是否可点击可以通过修改对象的属性进而修改网页控件的属性,有利于代码的复用和全局的管理.

<template>
  <div v-bind:class="staus">div测试</div>
  <button v-bind:disabled="isButtonDisabled">按钮</button>

  <div v-bind:="QWER">动态绑定</div>
</template>

<script>
export default {
  data() {
    return {
      staus: "active",
      isButtonDisabled: false,
      QWER: {
        id: "mzh",
        class: "191",
      },
    };
  },
};
</script>

<style>
.active {
  color: red;
  font-size: larger;
}
</style>

在这里插入图片描述


总结

大家喜欢的话,给个👍,点个关注!给大家分享更多计算机专业学生的求学之路!

版权声明:

发现你走远了@mzh原创作品,转载必须标注原文链接

Copyright 2024 mzh

Crated:2024-3-1

欢迎关注 『VUE』 专栏,持续更新中
欢迎关注 『VUE』 专栏,持续更新中
『未完待续』


  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发现你走远了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值