7。 V-bind 繫結
阿新 • • 發佈:2021-06-25
v-bind介紹
之前學習Mustache表示式 一般用在標籤體中,然而有有些標籤的內容是響應式的,就無法用Mustache來解析,例如:
ima 和 a標籤:
<script src="js/vue.js"></script> <div id="app"> <a href="{{baidu}}">百度一下</a> <img src="{{imgUrl}}"> </div> <script> 此程式碼是錯誤程式碼×××const app = new Vue({ el:"#app", data:{ baidu:"https://www.baidu.com/", imgUrl:"https://www.baidu.com/img/pc_a91909218349e60ed8f6f6f226c30e5f.gif" } }) </script>
所以這個程式碼是完全錯誤的 在標籤屬性中是解析不了的 若我們想讓他可以實現解析【繫結】,也讓標籤屬性實現動態化:
那麼就用v-bind指令:
作用:動態繫結屬性
縮寫::
預期:any (with argument) | Object (without argument)
引數:attrOrProp (optional)
即:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <script src="js/vue.js"></script> <div id="app"> <!-- v-bind: --> <av-bind:href="baidu">百度一下</a> <img v-bind:src="imgUrl"> </div> <script> const app = new Vue({ el:"#app", data:{ baidu:"https://www.baidu.com/", imgUrl:"https://www.baidu.com/img/pc_a91909218349e60ed8f6f6f226c30e5f.gif" } }) </script> </body> </html>
所以就成功了,還有簡寫 “:” 例如
<div id="app"> <!-- v-bind: 簡寫就是 : 冒號即可 --> <a :href="baidu">百度一下</a> <img :src="imgUrl"> </div> <script> const app = new Vue({ el:"#app", data:{ baidu:"https://www.baidu.com/", imgUrl:"https://www.baidu.com/img/pc_a91909218349e60ed8f6f6f226c30e5f.gif" } }) </script>
總結:
很方便的一個繫結 但是呢 注意 後面跟的資料不是 {{}} ,而是Vue的data中的鍵.