一、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. Jmeter 自动化测试报告扩展(转 Todo 需要修正)

    首先了解下生成测试报告的过程,我们看到的测试报告是由.jtl格式转换为.html,html报告的样式由extras目录下xsl文件决定.优化测试报告需要分为两部分内容,首先我们要优化输出的测试内容,其 ...

  2. js常见问题之为什么点击弹出的i总是最后一个

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. codeforces round 474 pathwalks

    题目传送门http://codeforces.com/contest/960/problem/F 4月25号期中考,答应过年级组长要考年排前3的,所以25号以前我就不搞竞赛了,期中考要考好. 有很多大 ...

  4. Docker基础 :网络配置详解

    本篇文章将讲述 Docker 的网络功能,包括使用端口映射机制来将容器内应用服务提供给外部网络,以及通过容器互联系统让多个容器之间进行快捷的网络通信,有兴趣的可以了解下. 大量的互联网应用服务包含多个 ...

  5. C#基础之基本类型

    本丝花了近半年,终于将<CLR Via C#>这本书看完了(请不要BS本人的看书速度T_T),这确实是一本好书,大大们推荐的果然值得一读. 虽然很多东西还没有尽得其要,我常想在自己深刻掌握 ...

  6. java——变量、jvm内存划分

    基本数据变量类型:byte.short.int.long.float.double.boolean.char eg : int i = 1; 引用数据变量类型:数组.类.接口.枚举.注解 eg : S ...

  7. jquery——无缝滚动

    无缝滚动: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. 021 Merge Two Sorted Lists 合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. 牛客网Java刷题知识点之基本类型、引用类型

    不多说,直接上干货! byte-short-int-long,方便识记.

  10. java编程如何实现2017-01-16 22:28:26.0这样的时间数据,转换成2017:01:16:22:28:26这样的时间数据

    不多说,直接上干货! timereplace.java package zhouls.bigdata.DataFeatureSelection.util; /* * 这个程序,是用来做补充的 */ p ...