本文實例講述了vue基礎之v-bind屬性、class和style用法。分享給大家供大家參考,具體如下:
一、屬性
屬性:
v-bind:src=""
width/height/title....
簡寫:
:src="" 推薦
<img src="{{url}}" alt=""> 效果能出來,但是會報一個404錯誤<img v-bind:src="url" alt=""> 效果可以出來,不會發404請求
window.onload=function(){ new Vue({ el:'#box', data:{ url:'https://www.baidu.com/img/bd_logo1.png', w:'200px', t:'這是一張美麗的圖片' }, methods:{ } }); };<div id="box"> <!--<img src="{{url}}" alt="">--> <img :src="url" alt="" :width="w" :title="t"> </div>二、class和style
:class="" v-bind:class=""
:style="" v-bind:style=""
:class="[red]" red是數據
:class="[red,b,c,d]"
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> .red{ color: red; } .blue{ background: blue; } </style> <script src="vue.js"></script> <script> window.onload=function(){ new Vue({ el:'#box', data:{ claOne:'red',//這里的red是樣式class類名 claTwo:'blue' }, methods:{ } }); }; </script></head><body> <div id="box"> <!--這里的calOne,calTwo指data里的數據--> <strong :class="[claOne,claTwo]">文字...</strong> </div></body></html>:class="{red:true, blue:false}"http://這里是{ json}
<style> .red{ color: red; } .blue{ background: blue; } </style> <script src="vue.js"></script> <script> window.onload=function(){ new Vue({ el:'#box', data:{ }, methods:{ } }); }; </script><div id="box"> <strong :class="{red:true,blue:true}">文字...</strong> </div><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> .red{ color: red; } .blue{ background: blue; } </style> <script src="vue.js"></script> <script> window.onload=function(){ new Vue({ el:'#box', data:{ a:true, b:false }, methods:{ } }); }; </script></head><body> <div id="box"> <strong :class="{red:a,blue:b}">文字...</strong> </div></body></html>
新聞熱點
疑難解答
圖片精選