vue数据绑定

11 篇文章 0 订阅
数据绑定:
<div id="app">
数据渲染:
  {{ message }}   //数据绑定
  <div v-html="htmlMess"></div>    //html绑定
  <div v-text="message"></div>    //数据绑定


属性绑定:v-bind
  <h1 v-bind:title="message">aaa</h1>      //属性绑定
  <a v-bind:href="url">百度</a>    //属性绑定
  <a :href="url">百度</a>    //属性绑定简写


类名绑定:
  <div v-bind:class="active : isActive"></div>    //类绑定,当isActive为true时类名生效
  <div v-bind:class="active:isActive,red:isRed"></div>    //多类名绑定,用逗号隔开
  <div v-bind:class="classObj"></div>    //对象类名绑定
  <div v-bind:class="[active,red]"></div>    //类名数组绑定
  <div v-bind:class="isActive ? active : red"></div>    //三元运算符类名绑定


样式绑定:
  <div v-bind:style="{width:width,height:height}"></div> //内联样式绑定
  <div v-bind:sytle="styleObj"></div> //内联样式对象绑定
  <div v-bind:style="[styleObj1,styleObj2]"></div>         //内联样式对象数组绑定




条件绑定
  <p v-if="seen">hahah</p>    //条件为真时显示,css中直接有无此元素
  <p v-show="seen">hah</p>     //效果同if,css中为display:none
循环绑定:
  <p v-for="list in lists">{{alist.text}}</p>    //绑定lists数组
  <p v-for="value in lists">{{value}}</p>     //值循环输出
  <p v-for="(key value) in lists">{{key}}:{{value}}</p>     //键值对输出
  <p v-for="(index key value) in lists">{{index}}:{{key}}:{{value}}    //索引加键值对输出
  <p v-for="n in 10">{{n}}</p>    //整数迭代


事件绑定:
  <a v-on:click="fun1">点击</a>    //事件对应fun1方法 
  <a @click="fun1">点击</a>     //事件绑定简写
  事件修饰符:
  <!-- 阻止单击事件冒泡 -->
  <a v-on:click.stop="doThis"></a>
  <!-- 提交事件不再重载页面 -->
  <form v-on:submit.prevent="onSubmit"></form>
  <!-- 修饰符可以串联  -->
  <a v-on:click.stop.prevent="doThat"></a>
  <!-- 只有修饰符 -->
  <form v-on:submit.prevent></form>
  <!-- 添加事件侦听器时使用事件捕获模式 -->
  <div v-on:click.capture="doThis">...</div>
  <!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 -->
  <div v-on:click.self="doThat">...</div>
  <!-- click 事件只能点击一次,2.1.4版本新增 -->
  <a v-on:click.once="doThis"></a>


按键绑定:相应按键按下时触发
  <input v-on:keyup.enter="submit">
  <input @keyup.enter="submit">   //缩写
  全部的按键别名:
  .enter  .tab  .delete (捕获 "删除" 和 "退格" 键) .esc  .space  .up  .down  .left  .right  .ctrl  .alt  .shift  .meta


双向绑定:
  <p>{{message}}</p>    //绑定Vue中message
  <input type="text" v-model="message"/>    //input输入值将传入Vue中的message
  <select v-model="message" id="aa">      //同input
  <option>百度</option>
  <option>腾讯</option>
  <option>阿里</option>
  </select>
  绑定修饰符:v-model.lazy//将input同步改为change同步    v-model.mumber//将能转化为数字的字符串转化为数字
  v-model.trim//过滤空格


运算绑定:  
<p>{{reverseMess}}<p/>      //相当于调用方法,此方法对message取反
</div>


<script>
  var app = new Vue({
    el: '#app',     //绑定元素
    data: { //绑定数据
    message: 'Hello Vue!',
    isActive:true,
    isRed:true,
    width:"200px",
    height:"300px",
    styleObj:{
    color:red,
    height:"200px"
    }
    classObj:{
    isActive:true,
    isRed:true
   }
    htmlMess:'<h2>hello world</h2>',
    seen:false,
    url:'http://www.baidu.com',
    lists:[
      { text: 'Learn JavaScript' },
      { text: 'Learn Vue' },
      { text: 'Build something awesome' }
    ]
    }
   }
  })
</script>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Vue数据绑定的数字时钟是一种利用Vue框架实现的动态显示时间的时钟。通过Vue数据绑定机制,可以实时更新时钟的显示内容,使其能够自动更新。 实现基于Vue数据绑定的数字时钟的步骤如下: 1. 创建一个Vue实例,并定义一个data属性来存储当前时间。 2. 使用Vue的生命周期钩子函数created,在实例创建完成后,通过JavaScript的Date对象获取当前时间,并将其保存在data属性中。 3. 在Vue的模板中,使用双花括号语法(Mustache语法)将data属性中的时间值绑定到页面上,实现实时显示。 4. 使用JavaScript的定时器函数setInterval,每秒更新一次data属性中的时间值,从而实现时钟的动态更新。 下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Vue数字时钟</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="clock"> <h1>{{ currentTime }}</h1> </div> <script> new Vue({ el: '#clock', data: { currentTime: '' }, created: function() { this.updateTime(); setInterval(this.updateTime, 1000); }, methods: { updateTime: function() { var date = new Date(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); this.currentTime = hours + ':' + minutes + ':' + seconds; } } }); </script> </body> </html> ``` 通过上述代码,我们可以实现一个基于Vue数据绑定的数字时钟。每秒钟,时钟会自动更新显示当前的时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值