视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
vue基础之v-bind属性、class和style用法分析
2020-11-27 22:00:12 责编:小采
文档

本文实例讲述了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}"//这里是{ 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>

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="vue.js"></script>
 <script>
 window.onload=function(){
 new Vue({
 el:'#box',
 data:{
 json:{
 red:true,
 blue:true
 }
 },
 methods:{
 }
 });
 };
 </script>
</head>
<body>
 <div id="box">
 <strong :class="json">文字...</strong>
 </div>
</body>
</html>

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="vue.js"></script>
 <script>
 window.onload=function(){
 new Vue({
 el:'#box',
 data:{
 c:{color:'red'},//这里的red是 class .red
 b:{backgroundColor:'blue'}//注意: 复合样式,采用驼峰命名法
 },
 methods:{
 }
 });
 };
 </script>
</head>
<body>
 <div id="box">
 <strong :style="[c,b]">文字...</strong>
 </div>
</body>
</html>

:style="json"

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title></title>
 <style></style>
 <script src="vue.js"></script>
 <script>
 window.onload=function(){
 new Vue({
 el:'#box',
 data:{
 a:{
 color:'red',
 backgroundColor:'gray'
 }
 },
 methods:{
 }
 });
 };
 </script>
</head>
<body>
 <div id="box">
 <strong :style="a">文字...</strong>
 </div>
</body>
</html>

希望本文所述对大家vue.js程序设计有所帮助。

下载本文
显示全文
专题