vue基础语法:插值,绑定属性:(v-bind)

二、vue基础语法:

1、插值(即 {{ }} ):让内容动态显示

例如:span>Message: {{ msg }}span>

指令:v-once:执行一次性的插值,内容不会随着更新

<span v-once>{{ msg }}</span>//这个msg内容将不会动态改变

v-text:渲染文本,会覆盖标签的所有内容

v-pre:展示标签内容,不会进行解析

例如:<span v-pre>{{msg}} </span>//结果显示为:{{msg}}

2、绑定属性:(v-bind)

v-bind:用户绑定一个或者多个属性值,或者在组件间传值,简写为冒号(:)

(1)图片路径src和网址连接href的绑定

例如:<img :src="Url" /> <a :href="Href">点击</a>

(2)类名class绑定

例如:<div v-bind:class="{ active: isActive }"></div>
data: {
  isActive: true
}

上面active类名是否有效,取决于isActive是否为true

如果类名有多个,中间用逗号隔开,例如

<div class="static" v-bind:class="{ active: isActive, bg: isShow }"></div>
data: {
  isActive: true,
  isShow:false
}
结果为:<div class="static active"></div>,只有active类名有效,bg类名不会显示

还可以用obj方式:例如

<div v-bind:class="classObj"></div>
data: {
  classObj: {
    active: true,
    isShow:false
  }
}

或者使用返回对象的计算属性,例如

<div v-bind:class="classObject"></div>
data: {
  isActive: true,
  isShow:false
},
computed: {
  classObject: function () {
    return {
      active: this.isActive && !this.isShow,
      bg: this.isShow
    }
  }
}

或者使用返回对象的方法,例如

<div v-bind:class="IsShow()"></div>
data: {
  isActive: true,
  isShow:false
},
methods: {
  IsShow() {
    return {
      active: this.isActive && !this.isShow,
      bg: this.isShow
    }
  }
}

或者使用数组的方法,例如

<div v-bind:class="[activeClass, bgClass]"></div>
data: {
  activeClass: 'active',
  bgClass: 'bg'
}
结果为:<div class="active bg"></div>

或者三元表达式,例如

<div v-bind:class="[isActive?activeClass:'', bgClass]"></div>
data: {
  isActive: true,
  activeClass: 'active',
  bgClass: 'bg'
}
结果为:<div class="active bg"></div>

(2)内联样式style绑定

v-bind:style="",例如

<div v-bind:style="{ color: color, fontSize: fontSize + 'px' }"></div> 
data: {
  color: '#000',
  fontSize: 20
}
或者写成obj
<div v-bind:style="style"></div> 
data: {
  style:{
      color: '#000',
      fontSize: '20px'  
  }
}

也可以使用对象的计算属性和方法,或者数组形式来使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值