一、vue的监听

1.监听的例子

如:

html:<input type="number" v-model="a" />

js:

watch: {
//监听完整写法
// a: {
// handler: function(){
// console.log('a变化了2');
// }
// }
//a属性如果发生了变化,a配置函数就会执行
//简写
a: function (newVal, oldVal) {
console.log('a变化了1');
console.log(newVal, oldVal);
}
}
2.vue写一个定时器
methods: {
A: function () {
setInterval(() => {
this.obj.today = new Date;
}, 1000)
}
},
mounted: function () {
this.A();
}
3.vm实例的监听
//vm实例的方法,对vm的属性即相关的函数执行监听
// 参数1:监听的值
// 参数2:回调
vm.$watch(function(){
//this指向实例对象
var count = this.a + this.b;
return '';
}, function(newVal, oldval){
console.log("变化了");
console.log(newVal, oldval);
}, {
deep: true
});
二、实例的生命周期
methods: {
testAction(){
console.log('testAction调用');
},
modifyAction(){
console.log('修改');
this.message = 'hello world';
//属性发生变化,监听dom更新完成的事件
// 属性修改后,不要任何代码,执行监听
this.$nextTick(()=>{
console.log('nextTick执行');
});
console.log(this.message);
console.log(this.$refs.info1.innerText);
//更新轮播,滚动
},
updateAction(){
this.$forceUpdate();
}
},
//实例创建前,什么也做不了
beforeCreate(){
console.log('beforeCreate执行了');
// console.log(this.message);
// this.testAction();
},
created(){
console.log('created执行了');
// this.obj.name = '123456';
},
beforeMount(){
console.log('beforeMount执行了');
// console.log(document.querySelector('.info1').innerText);
// console.log(this.$refs);
// console.log(this.$refs.info1);
// console.log(this.$refs.info2);
},
mounted(){
console.log('mounted执行了');
console.log(document.querySelector('.info1').innerText);
console.log(this.$refs);
},
// 更新前,dom更新前
beforeUpdate(){
console.log('beforeUpdate执行了');
console.log(this.message);
},
//更新完成,dom更新后
updated() {
console.log('updated执行了');
console.log(this.message);
//更新轮播,滚动的事件
},
beforeDestroy(){
console.log('beforeDestroy执行了');
},
destroyed(){
console.log('destroyed执行了');
}
});
/*
创建:
1.new Vue();
2.读取生命周期函数
3.beforeCreate()
4.加载data,computed,watch,methods....添加属性的数据观测
5.created()
挂载:
6.判断是否有$sel/等待$mount()调用
7.beforeMount()
8.渲染dom结构
9.mounted()
//在mounted之后操作dom结构,但是不用使用document访问dom。
// 操作dom的方式使用ref给dom赋值,通过$refs访问
 
更新:更新的钩子函数中不要修改属性。
10.属性发生了变化
11.beforeUpdate() dom更新前
12.重新渲染dom,dom进行更新
13.updated() dom更新完毕
//如果数据变化,要操作更新后的dom结构,使用$nextTick()
// 销毁:
14:beforeDestroy()
15.移除事件监听,绑定
16.destroyed()
$refs:获得vm实例作用下的dom
$nextTick()数据发生变化后,监听dom更新完毕的事件
$forceUpdate()强制更新dom

vue的属性监听的更多相关文章

  1. vue对象属性监听

    对象属性监听的两种方法: 1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValu ...

  2. Vue 变量,成员,属性监听

    Vue变量 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...

  3. Vue之计算属性Computed和属性监听Watch,Computed和Watch的区别

    一. 计算属性(computed) 1.计算属性是为了模板中的表达式简洁,易维护,符合用于简单运算的设计初衷. 例如: <div id="app"> {{ myname ...

  4. Vue之watch监听对象中某个属性的方法

    新建 userinfo = { name: "小明",  age: "18", } vue中watch监听name的方法 1. 可以结合计算属性的方法实现 { ...

  5. Vue.js之计算属性(computed)、属性监听(watch)与方法选项(methods)

    vue.js官网:https://cn.vuejs.org/v2/guide/components-registration.html 一.计算属性-computed 1. 作用:能够避免数据冗余,通 ...

  6. 微信小程序实现watch属性监听数据变化

    Vue 提供了一种通用的方式来观察和响应 Vue 实例上的数据变动:监听属性 watch. 虽然watch的滥用会导致性能不佳,但在一些情况下我们还是需要watch,使得代码更加简洁.逻辑更加清晰(其 ...

  7. Vue watch 深层监听

    Vue中监听某个对象的属性 为了避免监听整个对象导致效率问题,可以监听某个对象的特定属性 watch: { 'deptModel.depts': { handler(newVal, oldVal) { ...

  8. vue2.x版本中Object.defineProperty对象属性监听和关联

    前言 在vue2.x版本官方文档中 深入响应式原理 https://cn.vuejs.org/v2/guide/reactivity.html一文的解释当中,Object.defineProperty ...

  9. vue mounted中监听div的变化

    vue mounted中监听div的变化 <div style="width:200px;height:30px;background: #0e90d2" id=" ...

随机推荐

  1. STP-19-Port-Channel发现和配置

    工程师在给一台交换机上的特定Port-Channel增加多个端口时,有一些配置参数必须相同,如下所示:   使用相同的速率和双工设置: 使用相同的操作模式(Trunk.Access.动态): 若不为T ...

  2. js 数组,字符串,json互相转换(在select实现多个输入的时候与后台交互常使用)

    数组转字符串 var arr = [1,2,3,4,'巴德','merge']; var str = arr.join(','); console.log(str); // 1,2,3,4,巴德,me ...

  3. webissue 搭建 issue 分析工具

    http://www.cnblogs.com/feiyun8616/p/6208423.html

  4. 物理机和虚拟机互相可以ping通,还是无法连接

    关闭防火墙服务 CentOS # systemctl stop firewalld.service Debian # iptables -F Ubuntu # ufw disable 安装SSH服务 ...

  5. 来自于51CTO的经典学习资料汇总

    移动开发类: 1.2012Android开发热门资料(110个)       http://bbs.51cto.com/thread-934023-1.html 2.[绝对给力]Android开发免豆 ...

  6. [COGS 347]地震

    时间限制:4 s   内存限制:128 MB 问题描述 某国地形狭长,中部有一列山脉,由于多发地震,山脉在不断变化中.地震发生时,山脉有可能发生如下变化:局部海拔升高或降低,板块运动产生地裂而出现一段 ...

  7. Windows下使用beego遇到的问题

    一.解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in: 参考链接:http://blog.csdn.net/mecho/art ...

  8. uLua学习之使用协程(终)

    前言 今天是本系列的第六篇文章,也是最后一篇,我们来看看uLua中如何来实现协程吧.首先,让我们明确协程的概念.在百度百科上的是这样说的,协程更适合于用来实现彼此熟悉的程序组件,如合作式多任务,迭代器 ...

  9. Bugzilla-5.0.3 (OpenLogic CentOS 7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.6 bugzilla5.0.3 mariadb5.5.47 perl5.16.3 apache bug tracking bug ...

  10. ionic文本颜色

    需添加"ion-text"使"color='light'"生效 <div text-center ion-text color="light&q ...