一、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. Ubuntu16.04双网卡绑定

    服务器经常有多个网卡,为了保证网络冗余性,一个网卡出现故障时,不导致网络服务中断,可以懂多网卡网卡绑定来解决此问题. 环境: 系统:Ubuntu16.04 网卡:em1 em2 ip:192.168. ...

  2. urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

    解决办法: import ssl # 在请求之前加上 ssl._create_default_https_context = ssl._create_unverified_context

  3. CF24D Broken robot 后效性DP

    这题咕了好久..... 设$f[i][j]$表示从$(i,j)$到最后一行的期望步数: 则有 $ f[i][1]=\frac{1}{3}(f[i][1]+f[i][2]+f[i+1][1])+1$ $ ...

  4. 1101 Quick Sort(25 分

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  5. JDK 7 API 下载

    JDK 7 API 官方下载地址: http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download ...

  6. SpringBoot | 第十九章:web应用开发之WebSocket

    前言 web开发也讲解了三章了,这章节开始讲解关于与前端通信相关知识.实现一个在线聊天室类似的功能或者后端推送消息到前端,在没有WebSocket时,读大学那伙还有接触过DWR(Direct Web ...

  7. Srping MVC中Controller的void方法

    第一种 通过修改response来修改页面 /** * 方式一:通过声明HttpServletResponse类型的方法入参,来使用HttpServletResponse对象. * 注意:在Contr ...

  8. asp.net MVC 4.0 Controller回顾——ModelBinding实现过程

    以DefaultModelBinder为例 为简单模型绑定(BindSimpleModel)和复杂模型绑定(BindComplexModel) public virtual object BindMo ...

  9. java-类的定义和用法

    1.类的定义 public class Human{ }//每个源文件必须也只能有一个public类 class boy{ }//可以定义多个class类 class girl{ } 上面的类定义好后 ...

  10. 属性,选择器和css

    一.属性 属性:表示事物的一些特征 属性分为标签属性和样式属性 标签属性:<img src="1.jpg" width="200px" heifht=&q ...