一、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. 牛客假日团队赛1 I.接机

    链接: https://ac.nowcoder.com/acm/contest/918/I 题意: 一场别开生面的牛吃草大会就要在Farmer John的农场举办了! 世界各地的奶牛将会到达当地的机场 ...

  2. 装饰器(Decorator)模式

    public interface IDoThings { public void doSomeThing(); } public class DoThings implements IDoThings ...

  3. Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告2(使用PyCharm )

    1.说明 在我前一篇文件(Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使用IDLE ))中简单的写明了,如何生产测试报告,但是使用IDLE很麻烦, ...

  4. Carryon的字符串

    I J I: Carryon的字符串 时间限制: 1 s      内存限制: 128 MB      提交 我的状态 题目描述 Carryon最近喜欢上了一些奇奇怪怪的字符,字符都是英文小写字母,但 ...

  5. jdbc 开启事务

    package com.itheima.tx; import java.sql.Connection; import java.sql.PreparedStatement; import java.s ...

  6. MySQL导入大sql 文件大小限制问题的解决

    解决过程如下: 1.由于mysql能解析sql的压缩文件,因此将200M压缩后为5M. 2.默认情况下:MySQL导入文件大小有限制的,最大为2M,所以当文件很大时候,直接无法导入,可修改php.in ...

  7. IE浏览器与非IE浏览器JS日期兼容性问题处理

    执行语句 console.log(new Date("2017-07-04 18:40").getTime()); 在IE浏览器中打印出:NAN 在非IE浏览器中打印出:14991 ...

  8. cout格式化输出 详解

    //在使用setf等库函数时使用 //在使用流操纵算子时使用 //using namespace std; //以下所有的setf()都有对应的unsetf()用于取消设置 //所有的setiosfl ...

  9. GCC 编译错误 relocation truncated to fit: R_X86_64_32S against `.bss'

    问题如下图所示:(.text+0x53a): relocation truncated to fit: R_X86_64_32S against `.bss' 以前在Linux中编译程序,从来没有遇到 ...

  10. Android RxJava2+Retrofit2单文件下载监听进度封装

    RxJava2和Retrofit2用的越来越多,最近也在封装一个通用的网络请求库,其中就包括了单文件下载的方法,所以这里进行记录.文末附带Demo 由于网上很多的方法都是使用拦截器进行进度的监听,个人 ...