vue 计算属性和监听器
一、计算属性
模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的。在模板中放入太多的逻辑会让模板过重且难以维护。例如:
<div>
{{ message.split('').reverse().join('') }}
</div>
在这个地方,模板不再是简单的声明式逻辑。你必须看一段时间才能意识到,这里是想要显示变量 message 的翻转字符串。当你想要在模板中多次引用此处的翻转字符串时,就会更加难以处理。
所以,对于任何复杂逻辑,你都应当使用计算属性。
<div id = 'com'>
<h3>{{msg}}</h3>
<p>{{currentMsg}}</p>
<button @click='clickHandler'>更改</button>
</div>
var com = new Vue({
el:'#com',
data:{
msg:'学习computed'
},
methods:{
clickHandler(){
this.msg = '我更改了数据'
}
},
computed:{
currentMsg(){
// computed方法中默认只有getter
return this.msg
}
}
})
computed是计算属性,vue会一直监听着里面的变量,如果你的变量发生了变化,computed会立刻执行getter方法
重新渲染到页面上.
这也是vue的核心思想数据驱动视图
计算属性默认只有getter,当然如果你需要也可以设置setter
var com = new Vue({
el:'#com',
data:{
msg:'学习computed'
},
methods:{
clickHandler(){
this.currentMsg = '我更改了数据'
}
},
computed:{
currentMsg:{
set : function(newValue){
this.msg = newValue;
},
get : function(){
return this.msg;
}
}
}
})
示例一:轮播图:
<div id="app">
<div>
<img :src="currentImg" alt="" @mouseenter="stoplunbo()" @mouseleave="startlunbo()">
<ul>
<li v-for="(item,index) in ImgAttr" @click="liChangeImg(item)">{{index+1}}</li>
</ul>
</div>
<button @click="nextImg()">下一张</button>
<button @click="prevImg()">上一张</button> </div>
var app = new Vue({
el:'#app',
//所有的数据都放在数据属性里
data:{
currentNum:0,
currentImg:'./1.jpg',
str:'<p>哈哈哈</p>',
ImgAttr:[
{id:1,src:'./1.jpg'},
{id:2,src:'./2.jpg'},
{id:3,src:'./3.jpg'},
{id:4,src:'./4.jpg'}
],
Timer:null,
},
created(){
this.Timer = setInterval(this.nextImg,2000)
},
methods:{
//单体模式
clickHandler(){
//this 就是app实例化对象
this.show=!this.show;
},
ChangeColor(){
this.isRed=!this.isRed;
},
nextImg(){
if (this.currentNum==this.ImgAttr.length-1 ){
this.currentNum=-1;
}
this.currentNum++;
this.currentImg=this.ImgAttr[this.currentNum].src;
},
liChangeImg(item){
this.currentNum=item.id-1;
this.currentImg=item.src;
},
stoplunbo(){
clearInterval(this.Timer);
},
startlunbo(){
this.Timer = setInterval(this.nextImg,2000)
},
prevImg(){
if (this.currentNum == 0){
this.currentNum=this.ImgAttr.length-1;
}
this.currentNum--;
this.currentImg=this.ImgAttr[this.currentNum].src;
}
}
})
当然这里没有用到计算属性,如果用到了计算属性,也可以进行优化:
示例二:
<div id="app">
<div>{{showli}}</div>
<ul>
<li v-for="(item,index) in songs" @click="changename(index)">
<p>name:{{item.name}} author:{{item.author}}</p>
</li>
</ul>
<button @click="additem()">additem</button>
</div>
<script>
var songs=[
{'id':1,'src':'1.mp3','author':'chen1','name':'桃花朵朵开1'},
{'id':2,'src':'2.mp3','author':'chen2','name':'桃花朵朵开2'},
{'id':3,'src':'3.mp3','author':'chen3','name':'桃花朵朵开3'},
{'id':4,'src':'4.mp3','author':'chen4','name':'桃花朵朵开4'}
]; var app=new Vue({
el:'#app',
data:{
songs:songs,
currentIndex:0,
},
methods:{
changename(index){
this.currentIndex=index;
},
additem(){
this.songs.push({'id':5,'src':'5.mp3','author':'chen5','name':'桃花朵朵开5'})
}
},
computed:{
showli(){
return this.songs[this.currentIndex].name
}
}
})
</script>
这里需要说的是,在computed里面的变量,如果发生了改变,那么就会执行相应的getter,在驱动视图的改变.
vue 计算属性和监听器的更多相关文章
- Vue计算属性
github地址:https://github.com/lily1010/vue_learn/tree/master/lesson06 一 计算属性定位 当一些数据需要根据其它数据变化时,这时候就需要 ...
- 在做vue计算属性,v-for处理数组时遇到的一个bug
bug: You may have an infinite update loop in a component render function 无限循环 需要处理的数组(在 ** ssq **里): ...
- vue教程2-03 vue计算属性的使用 computed
vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...
- vue 计算属性 实例选项 生命周期
vue 计算属性: computed:{} 写在new vue()的属性,只要参与运算,数据不发生变化时,次计算只会执行一次,结果缓存,之后的计算会直接从缓存里去结果.如果其中的值发生变化(不管几个) ...
- Vue计算属性缓存(computed) vs 方法
Vue计算属性缓存(computed) vs 方法 实例 <div id="example"> <p>Original message: "{{ ...
- vue 计算属性实现过滤关键词
效果 html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...
- Vue 计算属性 && 监视属性
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 & ...
- Vue.js-05:第五章 - 计算属性与监听器
一.前言 在 Vue 中,我们可以很方便的将数据使用插值表达式( Mustache 语法)的方式渲染到页面元素中,但是插值表达式的设计初衷是用于简单运算,即我们不应该对差值做过多的操作.当我们需要对差 ...
- VUE 学习笔记 四 计算属性和监听器
1.计算属性 对于任何复杂逻辑,你都应当使用计算属性 <div id="example"> <p>Original message: "{{ me ...
随机推荐
- 搭建elsticsearch集群 报错with the same id but is a different node instance解决办法
搭建elsticsearch集群 报错with the same id but is a different node instance解决办法 学习了:https://blog.csdn.net/q ...
- SQLiteDatabase中query、insert、update、delete方法参数说明
1.SQLiteDataBase对象的query()接口: public Cursor query (String table, String[] columns, String selection, ...
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下官方软件开启报错伺服未就绪怎么办
一般是伺服到电机的动力线没接好(请查看动力线接线是否正确) 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线 ...
- Vue-router(vue2.0)用法示例
一.新建3个组件 1./src/components/post.vue <template> <div> hello world! this is POST! </div ...
- TCO'10 Wildcard Round 1000pt
题目大意: 给定一个N*M的棋盘,棋子可以攻击其左右距离不超过K的棋子.问有多少种放法使得棋盘上的棋子不能互相攻击. N,M,K都在1到1000000000的范围内,结果对100003取模. 官方题解 ...
- 聚类分析算法及SAS实现
聚类分析是用户细分里面最为重要的工具,而用户细分则是整个精准营销里面的基础. 聚类分析方法分为: 层次法:可分为凝聚式和分列式,适用于观测数比较少的情形 1.凝聚式:将每个观测都归为一类,然后每次都将 ...
- flex-direction用法解释
语法: flex-direction:row | row-reverse | column | column-reverse 默认值:row 适用于:flex容器 继承性:无 动画性:否 计算值:指定 ...
- UICollectionView的header悬停
UICollectionView的header悬停,继承UICollectionViewFlowLayout,重写相关方法 // // StickyHeaderLayout.h // Wombat / ...
- C#中后台线程和UI线程的交互
在C#中,从Main()方法开始一个默认的线程,一般称之为主线程,如果在这个进行一些非常耗CPU的计算,那么UI界面就会被挂起而处于假死状态,也就是说无法和用户进行交互了,特别是要用类似进度条来实时显 ...
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
项目开发中的一些注意事项以及技巧总结 1.jquery采用ajax向后端请求时,MVC框架并不能返回View的数据,也就是一般我们使用View().PartialView()等,只能返回json以 ...