天天看点

vue指令 v-bind 用于绑定数据和元素属性

目录

        • 基础用法

基础用法

v-bind用于绑定数据和元素属性

语法糖简化为  :   等价于 v-bind:

可以绑定 href/class/style/src等多种属性


           
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <ul>
      <li v-for="u in users" :key="u.id">{{u.id}}--{{u.name}}</li>
    </ul>
    <ul>
      <li v-for="(u,index) in users" :key="u.id">{{index}}--{{u.id}}--{{u.name}}</li>
    </ul>
    <ul>
      <li v-for="(val, key, index) in userObj" :key="val">{{val}}--{{key}}--{{index}}</li>
    </ul>
    <div @click="pushFunc()">v-on语法糖</div>
    <div @click="pushFunc()">v-on</div>
    <div v-once>{{msg}}</div>
    <div @click="changeMsg()">v-once</div>
    <span v-html="aHtml"></span>
    <a v-bind:href="baiduHref">百度一下</a>
    <a :class="baiduClass" :href="baiduHref" :style="baiduStyle" >百度一下</a>

  </div>
</template>

<script>


export default {
  name: 'HelloWorld',
  data () {
    return {
      baiduHref: 'https://www.baidu.com',
      baiduStyle: {color: 'red', fontWeight: 'bold'},
      baiduClass: 'baidu-class'
    }
  },
  methods: {
  },
  async created () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.baidu-class {
  border: 1px solid black;
}
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

           
vue指令 v-bind 用于绑定数据和元素属性

继续阅读