v-bind的简略介绍

v-bind用于绑定一个或多个属性值,或者向另一个组件传递props值。目前,个人所用之中,更多的是使用于图片的链接src,a标签中的链接href,还有样式以及类的一些绑定,以及props传递的时候,在子组件的标签中所绑定。目前在做的项目中,有些例子,记录下。多数情况下,为了简洁,还是更喜欢写”:“这个语法糖来代替v-bind,下面的例子中都是写的这个。

v-bind绑定class
1.对象语法

//用法一:直接通过{}绑定一个类
<h2 :class="{'active': isActive}">Hello World</h2> //用法二:也可以通过判断,传入多个值
<h2 :class="{'active': isActive, 'line': isLine}">Hello World</h2> //用法三:和普通的类同时存在,并不冲突
//注:如果isActive和isLine都为true,那么会有title/active/line三个类
<h2 class="title" :class="{'active': isActive, 'line': isLine}">Hello World</h2> //用法四:如果过于复杂,可以放在一个methods或者computed中
//注:classes是一个计算属性
<h2 class="title" :class="classes">Hello World</h2>

2.数组语法

<h2 :class="['active']">Hello World</h2>
<h2 :class=“[‘active’, 'line']">Hello World</h2>
<h2 class="title" :class=“[‘active’, 'line']">Hello World</h2> //如果过于复杂,可以放在一个methods或者computed中
//注:classes是一个计算属性
<h2 class="title" :class="classes">Hello World</h2>

实际情况下,感觉目前敲的还是用对象语法会更多些。记忆中最深的是,在写tabbar组件的时候,为了实现tabbar中的文字,可以点击时候变颜色,使用了绑定属性。下方示例的active绑定了isactive,isactive为设置的一个计算属性,如果为true时,会激活active,使得对应的<div>模块,获得active属性。

<template>
<div class="eachtabbar" v-on:click="itemclick">
<div v-if="!isactive"><slot name="img-dark"></slot></div>
<div v-else> <slot name="img-light"></slot></div>
<div v-bind:class="{active:isactive}"> <slot name="text"></slot></div>
</div>
</template> <script>
export default {
name: "eachtabbar",
props:{
path:String
},
data(){
return{ }
},
computed:{
isactive(){
return this.$route.path==this.path
}
},
methods:{
itemclick(){
if(this.$route.path==this.path){
return
}
this.$router.push(this.path)
}
}
//if语句是为了处理相同路径报错问题
}
</script> <style scoped>
.eachtabbar{
flex: 1;
text-align: center;
height: 49px;
}
.eachtabbar img{
height: 24px;
width: 24px;
margin-top: 3px;
vertical-align: middle;
}
.active{
color: yellow;
}
</style>

v-bind绑定style

1.对象语法

:style="{color: currentColor, fontSize: fontSize + 'px'}",style后面跟的是一个对象类型。对象的key是CSS属性名称,对象的value是具体赋的值,值可以来自于data中的属性。

2.数组语法

<div v-bind:style="[baseStyles, overridingStyles]"></div>,style后面跟的是一个数组类型 多个值以,分割即可。

还是上一个例子,在写tabbar组件的时候,在想如果我不想每一块的点击都是一个颜色,那么怎么设置呢,因此使用了绑定style。计算属性中设置了一个activeStyle属性,且通过props默认若不更改情况下,默认设置为黄色。若要更改直接在标签中直接设置active-color即是。

<template>
<div class="tabbar-item" v-on:click="itemclick">
<div v-if="!isActive"><slot name="item-pic"></slot></div>
<div v-else><slot name="item-cli"></slot></div>
<div v-bind:style="activeStyle"><slot name="item-text"></slot></div></div>
</template> <script>
export default {
name: "tabbaritem",
props:{
path:String,
activeColor: {
type:String,
default:"yellow"
}
},
data(){
return{
// isActive:true
}
},
computed:{
isActive(){
return this.$route.path==this.path
},
activeStyle(){
return this.isActive? {color:this.activeColor}:{}
}
},
methods:{
itemclick(){
this.$router.push(this.path)
}
}
}
</script> <style scoped>
.tabbar-item{
flex: 1;
text-align: center;
height: 49px;
}
.tabbar-item img{
height: 24px;
width: 24px;
margin-top: 3px;
vertical-align: middle;
}
/*.active{*/
/* color: #bfbf0d;*/
/*}*/ </style>

vue的绑定属性v-bind的更多相关文章

  1. vue v-bind绑定属性和样式

    这期跟大家分享的,是v-bind指令.它可以往元素的属性中绑定数据,也可以动态地根据数据为元素绑定不同的样式. 绑定属性 最简单的例子,我们有一张图片,需要定义图片的src.我们可以直接在元素的属性里 ...

  2. vue 数据绑定 绑定属性 循环渲染数据

    <template> <!-- vue的模板里面 所有的内容要被一个根节点包含起来 --> <div id="app"> <h2>{ ...

  3. Vue 目录结构 绑定数据 绑定属性 循环渲染数据

    一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...

  4. React对比Vue(02 绑定属性,图片引入,数组循环等对比)

    import React, { Component } from 'react'; import girl from '../assets/images/1.jpg' //这个是全局的不要this.s ...

  5. vue 绑定属性 绑定Class 绑定style

    <template> <div id="app"> <h2>{{msg}}</h2> <br> <div v-bi ...

  6. Vue 目录结构分析 数据绑定 绑定属性 循环渲染数据 数据渲染

    一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...

  7. vue.js--基础 v-bind绑定属性使用

    背景:因为10月要休产假了,8月的时间我工作很少,因为最开始做平台我一直做的是后端,前端很少接触,所以现在有时间,就学习前端基础,前端使用的vue.js+element,因为没有基础,所以下了一个视频 ...

  8. Vue绑定属性 绑定Class 绑定style

    <template> <div id="app"> <h2>{{msg}}</h2> <br> <div v-bi ...

  9. 黑马vue---17、vue中通过属性绑定绑定style行内样式

    黑马vue---17.vue中通过属性绑定绑定style行内样式 一.总结 一句话总结: 如果属性名中带有短线必须加引号,比如: h1StyleObj: { color: 'red', 'font-s ...

随机推荐

  1. 利用Python特殊变量__dict__快速实现__repr__的一种方法

    在<第8.15节 Python重写自定义类的__repr__方法>.<Python中repr(变量)和str(变量)的返回值有什么区别和联系>.<第8.13节 Pytho ...

  2. 第14.12节 Python中使用BeautifulSoup解析http报文:使用select方法快速定位内容

    一. 引言 在<第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问>和<第14.11节 Python中使用BeautifulSo ...

  3. POJ2466 棋盘覆盖

    一张\(n*m\)的棋盘,有\(k\)个点不能被覆盖,问其余点能不能被\(1*2\)的小矩形完全覆盖,多测 这题先输入\(m\)是什么鬼啊!!! 其实是一个比较裸的二分图判定,把\(k\)个点挖去然后 ...

  4. 计算机语言与JAVA的发展

    计算机语言与JAVA的发展 第一代语言 2进制 第二代语言 汇编语言 解决人类无法读懂的问题 指令替代二进制 目前应用 逆向工程 机器人 病毒 第三代语言 摩尔定律 性能提升愈来愈慢 高级语言 面向过 ...

  5. viewer使用

    一款pc端,点击放大,缩放,翻转等功能的插件.

  6. 项目中对获取的数据进行下载成Excel表格

    //moment是操作日期的插件  //引入lodash是为了方便操作数据 //xlsx是获取表格的必须插件   import moment from 'moment'; import _ from  ...

  7. 使用Swiper快速实现3D效果轮播

    最近经常接到轮播图3D效果的需求, 特在此记录一下以备之后使用. 具体实现效果如下: 在这里介绍两种使用方式, 一种原生的html+php后端渲染, 一种是使用vue. 原生实现 引入 首先我们介绍原 ...

  8. MobaXterm无法退格删除

    MobaXterm退格删除出现^H,总是要取消输入重新敲语句,很麻烦 解决方法:打开MobaXterm-->settings-->Configuration,把"Backspac ...

  9. android adb命令* daemon not running.starting it now on port 5037 * 问题解决

    输入adb devices却出现了问题daemon not running.starting it now on port 5037, 2. 原因: adb的端口(5037)被占用了.至于这个5037 ...

  10. CET4词汇

    abandon vt.丢弃:放弃,抛弃 ability n.能力:能耐,本领 abnormal a.不正常的:变态的 aboard ad.在船(车)上:上船 abroad ad.(在)国外:到处 ab ...