一、计算属性

模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护。例如:

  1. <div>
  2. {{ message.split('').reverse().join('') }}
  3. </div>

在这个地方,模板不再是简单的声明式逻辑。你必须看一段时间才能意识到,这里是想要显示变量 message 的翻转字符串。当你想要在模板中多次引用此处的翻转字符串时,就会更加难以处理。

所以,对于任何复杂逻辑,你都应当使用计算属性。

  1. <div id = 'com'>
  2. <h3>{{msg}}</h3>
  3. <p>{{currentMsg}}</p>
  4. <button @click='clickHandler'>更改</button>
  5. </div>
  1. var com = new Vue({
  2. el:'#com',
  3. data:{
  4. msg:'学习computed'
  5. },
  6. methods:{
  7. clickHandler(){
  8. this.msg = '我更改了数据'
  9. }
  10.  
  11. },
  12. computed:{
  13. currentMsg(){
  14. // computed方法中默认只有getter
  15.  
  16. return this.msg
  17. }
  18. }
  19. })

computed是计算属性,vue会一直监听着里面的变量,如果你的变量发生了变化,computed会立刻执行getter方法

重新渲染到页面上.

这也是vue的核心思想数据驱动视图

计算属性默认只有getter,当然如果你需要也可以设置setter

  1. var com = new Vue({
  2. el:'#com',
  3. data:{
  4. msg:'学习computed'
  5. },
  6. methods:{
  7. clickHandler(){
  8. this.currentMsg = '我更改了数据'
  9. }
  10. },
  11. computed:{
  12. currentMsg:{
  13. set : function(newValue){
  14. this.msg = newValue;
  15. },
  16. get : function(){
  17. return this.msg;
  18. }
  19. }
  20. }
  21. })

示例一:轮播图:

  1. <div id="app">
  2. <div>
  3. <img :src="currentImg" alt="" @mouseenter="stoplunbo()" @mouseleave="startlunbo()">
  4. <ul>
  5. <li v-for="(item,index) in ImgAttr" @click="liChangeImg(item)">{{index+1}}</li>
  6. </ul>
  7. </div>
  8. <button @click="nextImg()">下一张</button>
  9. <button @click="prevImg()">上一张</button>
  10.  
  11. </div>
  1. var app = new Vue({
  2. el:'#app',
  3. //所有的数据都放在数据属性里
  4. data:{
  5. currentNum:0,
  6. currentImg:'./1.jpg',
  7. str:'<p>哈哈哈</p>',
  8. ImgAttr:[
  9. {id:1,src:'./1.jpg'},
  10. {id:2,src:'./2.jpg'},
  11. {id:3,src:'./3.jpg'},
  12. {id:4,src:'./4.jpg'}
  13. ],
  14. Timer:null,
  15.  
  16. },
  17. created(){
  18. this.Timer = setInterval(this.nextImg,2000)
  19. },
  20. methods:{
  21. //单体模式
  22. clickHandler(){
  23. //this 就是app实例化对象
  24. this.show=!this.show;
  25. },
  26. ChangeColor(){
  27. this.isRed=!this.isRed;
  28. },
  29. nextImg(){
  30. if (this.currentNum==this.ImgAttr.length-1 ){
  31. this.currentNum=-1;
  32. }
  33. this.currentNum++;
  34. this.currentImg=this.ImgAttr[this.currentNum].src;
  35. },
  36. liChangeImg(item){
  37.  
  38. this.currentNum=item.id-1;
  39. this.currentImg=item.src;
  40. },
  41. stoplunbo(){
  42. clearInterval(this.Timer);
  43. },
  44. startlunbo(){
  45. this.Timer = setInterval(this.nextImg,2000)
  46. },
  47. prevImg(){
  48. if (this.currentNum == 0){
  49. this.currentNum=this.ImgAttr.length-1;
  50. }
  51. this.currentNum--;
  52. this.currentImg=this.ImgAttr[this.currentNum].src;
  53.  
  54. }
  55. }
  56. })

当然这里没有用到计算属性,如果用到了计算属性,也可以进行优化:

示例二:

  1. <div id="app">
  2. <div>{{showli}}</div>
  3. <ul>
  4. <li v-for="(item,index) in songs" @click="changename(index)">
  5. <p>name:{{item.name}} author:{{item.author}}</p>
  6. </li>
  7. </ul>
  8. <button @click="additem()">additem</button>
  9. </div>
  1. <script>
  2. var songs=[
  3. {'id':1,'src':'1.mp3','author':'chen1','name':'桃花朵朵开1'},
  4. {'id':2,'src':'2.mp3','author':'chen2','name':'桃花朵朵开2'},
  5. {'id':3,'src':'3.mp3','author':'chen3','name':'桃花朵朵开3'},
  6. {'id':4,'src':'4.mp3','author':'chen4','name':'桃花朵朵开4'}
  7. ];
  8.  
  9. var app=new Vue({
  10. el:'#app',
  11. data:{
  12. songs:songs,
  13. currentIndex:0,
  14. },
  15. methods:{
  16. changename(index){
  17. this.currentIndex=index;
  18. },
  19. additem(){
  20. this.songs.push({'id':5,'src':'5.mp3','author':'chen5','name':'桃花朵朵开5'})
  21. }
  22. },
  23. computed:{
  24. showli(){
  25. return this.songs[this.currentIndex].name
  26. }
  27. }
  28. })
  29. </script>

这里需要说的是,在computed里面的变量,如果发生了改变,那么就会执行相应的getter,在驱动视图的改变.

vue 计算属性和监听器的更多相关文章

  1. Vue计算属性

    github地址:https://github.com/lily1010/vue_learn/tree/master/lesson06 一 计算属性定位 当一些数据需要根据其它数据变化时,这时候就需要 ...

  2. 在做vue计算属性,v-for处理数组时遇到的一个bug

    bug: You may have an infinite update loop in a component render function 无限循环 需要处理的数组(在 ** ssq **里): ...

  3. vue教程2-03 vue计算属性的使用 computed

    vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...

  4. vue 计算属性 实例选项 生命周期

    vue 计算属性: computed:{} 写在new vue()的属性,只要参与运算,数据不发生变化时,次计算只会执行一次,结果缓存,之后的计算会直接从缓存里去结果.如果其中的值发生变化(不管几个) ...

  5. Vue计算属性缓存(computed) vs 方法

    Vue计算属性缓存(computed) vs 方法 实例 <div id="example"> <p>Original message: "{{ ...

  6. vue 计算属性实现过滤关键词

    效果 html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...

  7. Vue 计算属性 && 监视属性

    1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 & ...

  8. Vue.js-05:第五章 - 计算属性与监听器

    一.前言 在 Vue 中,我们可以很方便的将数据使用插值表达式( Mustache 语法)的方式渲染到页面元素中,但是插值表达式的设计初衷是用于简单运算,即我们不应该对差值做过多的操作.当我们需要对差 ...

  9. VUE 学习笔记 四 计算属性和监听器

    1.计算属性 对于任何复杂逻辑,你都应当使用计算属性 <div id="example"> <p>Original message: "{{ me ...

随机推荐

  1. AForge.NET Framework-2.2.5

    http://www.aforgenet.com/framework/downloads.html AForge.NET Framework-2.2.5 简介 AForge.NET是一个专门为开发者和 ...

  2. [Debug] Diagnose a Slow Page Using Developer Tools

    Your page is showing high CPU usage and spinning up your laptop fan, but nothing is visibly happenin ...

  3. Linux学习笔记 (二)常用linux命令

    一.命令行语法: 命令字 [选项] [参数] 注意:Linux中对命令是区分大小写的. 二.获取命令帮助: 1.help命令:help xxx,shell内部指令,用来获取linux内部命令.例如:h ...

  4. Ubuntu16.04安装QQ

    说明:一开始,我在Ubuntu 16.04下安装的QQ版本是Wineqq2013SP6-20140102-Longene,但后来发现这个版本QQ在linux下问题很多,比如不能用键盘输入密码,QQ表情 ...

  5. <p>在我们的实际软件项目中,管理团队事实上比写代码或者实现一个客户的需求更为的有挑战性。由于编程实际上是和机器打交道,而和机器打交道,仅仅要你符合机器预定的逻辑,</p>

    在我们的实际软件项目中,管理团队事实上比写代码或者实现一个客户的需求更为的有挑战性. 由于编程实际上是和机器打交道.而和机器打交道,仅仅要你符合机器预定的逻辑, 一步步迈向解决这个问题的道路上一点都不 ...

  6. 基于React的PC网站前端架构分析

    代码地址如下:http://www.demodashi.com/demo/12252.html 本文适合对象 有过一定开发经验的初级前端工程师: 有过完整项目的开发经验,不论大小: 对node有所了解 ...

  7. android电话状态的监听

    电话状态的监听: /*** * 继承PhoneStateListener类,我们能够又一次其内部的各种监听方法 然后通过手机状态改变时,系统自己主动触发这些方法来实现我们想要的功能 */ class ...

  8. Spring 中的Null-Safety

    之前一直在某些代码中看到过使用@Nullable 标注过的注释,当时也没有在意到底是什么意思,所以这篇文章来谈谈Spring中关于Null的那些事. 在Java中不允许让你使用类型表示其null的安全 ...

  9. eclipse +cygwin+C++

    用Android eclipse做C++开发,一开始提示no binary的错误,貌似是因为没有编译二进制出来,我本机装了cygwin, 在命令台输入gcc,无显示,说明我没有把cygwin/bin的 ...

  10. Python调用API接口的几种方式 数据库 脚本

    Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...