本文实例讲述了vue基础之v-bind属性、class和style用法。分享给大家供大家参考,具体如下:
一、属性
属性:
v-bind:src="/UploadFiles/2021-04-02/"> width/height/title....
简写:
:src="/UploadFiles/2021-04-02/">
<img src="{{url}}"> 效果能出来,但是会报一个404错误
<img v-bind:src="url"> 效果可以出来,不会发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="/UploadFiles/2021-04-02/{{url}}">二、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="/UploadFiles/2021-04-02/vue.js">:class="{red:true, blue:false}"//这里是{ json}
<style> .red{ color: red; } .blue{ background: blue; } </style> <script src="/UploadFiles/2021-04-02/vue.js"><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> .red{ color: red; } .blue{ background: blue; } </style> <script src="/UploadFiles/2021-04-02/vue.js">data:{ json:{red:a, blue:false} }:class="json"
官方推荐用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> .red{ color: red; } .blue{ background: blue; } </style> <script src="/UploadFiles/2021-04-02/vue.js">style:
:style="[c]".red{ color: red; } <div id="box"> <strong :style="{color:'red'}">文字...</strong> </div>:style="[c,d]"
注意: 复合样式,采用驼峰命名法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style></style> <script src="/UploadFiles/2021-04-02/vue.js">:style="json"
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style></style> <script src="/UploadFiles/2021-04-02/vue.js">希望本文所述对大家vue.js程序设计有所帮助。