這篇文章要跟大家介紹的文章內容是關於4-v-bind指令的分析(附程式碼),有很好的參考價值,希望可以幫助到有需要的朋友。
1. 定義
1.1 v-bind 指令被用來回應地更新 HTML 屬性,其實它是支援單一 JavaScript 運算式 (v-for 除外)。
2. 文法
2.1 完整文法: >,解釋:v-bind 是指令,: 後面的 class 是參數,classProperty 則在官方文件中稱為「預期值」。
2.2 縮寫語法: >,解釋:: 後面的 class 是參數,classProperty 則在官方文件中被稱為“預期值” 。
3. 用法
3.1 綁定一個屬性
全寫程式範例:
<template><p> </p> <p>{{title}}</p> <span>{{text}}</span></template><script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", first: "span1", text: "绑定一个属性" } } }</script><style> .p1{ text-align: left; } .spancss1{ float: left; }</style>
<template> <div> <p class="p1">{{title}}</p> <span :value="first" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", first: "span1", text: "绑定一个属性" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <a :href="'http://'+first" class="spancss1">{{text}}</a> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", first: "www.baidu.com", text: '点击跳转到百度链接' } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:class="{prop1:isTrue,prop2:isActive}" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", isTrue: false, isActive: true, text: "对象语法1" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:class="obj" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", obj: { prop1: true, prop2: false }, text: "对象语法2" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:class="arr" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", arr: ['prop1','prop2','prop3'], text: "数组语法1" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:class="[prop1,prop2,prop3]" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", prop1: 'prop1', prop2: false, prop3: 'prop3', text: "数组语法2" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:class="[prop1?'prop1':'',prop2,prop3?'prop3':'',prop4?'prop4':'prop5',prop6?'prop6':'prop5']" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", prop1: false, prop2: 'prop2', prop3: true, prop4: true, prop6: false, text: "数组语法3" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:style="{background:color1,fontSize:fontSize+'px'}" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", color1: 'green', fontSize: 25, text: "绑定内联样式1" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:style="[prop1,prop2]" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", prop1: { background:'green' }, prop2: { fontSize: '25px', fontWeight: 'bolder' }, text: "绑定内联样式1" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:style="[prop1,prop2]" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", prop1: { background:'green' }, prop2: { fontSize: '25px', transform: 'rotate(7deg)' }, text: "绑定内联样式1" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
<template> <div> <p class="p1">{{title}}</p> <span v-bind:style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }" class="spancss1">{{text}}</span> </div> </template> <script> export default { name: "v-bindLearn", data() { return { title: "v-bind学习", text: "绑定内联样式4" } } } </script> <style scoped> .p1{ text-align: left; } .spancss1{ float: left; } </style>
總結:v-bind動態地綁定一個或多個特性,或一個元件prop 到表達式,可以很方便的渲染DOM
相關建議:
Vue中專案的初始建置流程(圖文)
Vue-cli建置專案後目錄結構的分析(圖文)
以上是4-v-bind指令的分析(附代碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!