插值語法不能作用在 HTML 特性上,因此使用 v-bind 指令
1、v-bind在一般特性上的使用:如id,src,disabled,checked,selected,name
html:
1 <section id="content">
2 插值語法不能作用在 HTML 特性上,遇到這種情況應該使用 v-bind 指令 3 <p v-bind:id="pId">信息</p>
4 <img v-bind:src="icon" alt="婚紗">
5 v-bind:disabled【縮寫::disabled】<br>
6 <button type="button" :disabled="isDisable">禁用</button>
7 </section>
js:
1 var vm = new Vue({ 2 el:"#content", 3 data: { 4 pId:"info", 5 icon:"../imgs/cloth.png", 6 isDisable:true
7 } 8 });
渲染的結果:
2、v-bind在class ,style上的使用
A: class 上的使用
html:
<section id="content"> 對象語法:<br> <span v-bind:class="{success:isSuccess}">當前狀態是否正確</span> <span v-bind:class="{success:isSuccess,disabled:true}">當前狀態是否正確</span> 數組語法<br> <span v-bind:class="[stateClass]">當前狀態是否正確</span> <span v-bind:class="[stateClass,{disabled:true}]">當前狀態是否正確</span> </section>
js:
1 var vm = new Vue({ 2 el:"#content", 3 data: { 4 pId:"info", 5 icon:"../imgs/cloth.png", 6 isDisable:true, 7 isSuccess:true, 8 stateClass:"success" 9 } 10 });
result:
B: style的使用
html:
1 對象語法:<br /> 2 <span v-bind:style="{color:activeColor}">當前狀態是否正確</span> 3 <span v-bind:style="{color:activeColor,fontSize:'18px'}">當前狀態是否正確</span><br> 4 數組語法<br /> 5 <span v-bind:style="colorObject">當前狀態是否正確</span> 6 <span v-bind:style="[colorObject,fontObject]">當前狀態是否正確</span>
js:
1 var vm = new Vue({ 2 el:"#content", 3 data: { 4 activeColor:"#009688", 5 colorObject:{ 6 color:"#009688" 7 }, 8 fontObject:{ 9 fontSize:"20px" 10 } 11 } 12 });
result: