computed属性和watch属性的区别之一【watch的基本用法】
来源:互联网 发布:java facet 是什么 编辑:程序博客网 时间:2024/05/21 18:45
<body> <div id = 'app'> <div> <div> <h3>{{title}}</h3> </div> <div> <h5>{{watch_string}}</h5> <div> <input type="text" v-model="mystring"/> </div> <div> <p>改变前的值:{{mystring_oldValue}}</p> <p>改变后的值:{{mystring_currentValue}}</p> </div> </div> <hr> <div> <h5>{{watch_function}}</h5> <div> <p><input type="text" v-model="mystring2"/></p> </div> <div> <p>改变前的值:{{mystring2_oldValue}}</p> <p>改变后的值:{{mystring2_currentValue}}</p> </div> </div> <hr> <div> <h5>{{watch_object}}</h5> <div> <!-- 当单观察数据myobject为对象时,如果键值发生变化, 为了监听到数据变化,需要添加deep:true参数 --> <p><input type="text" v-model="myobject.name"/></p> </div> <div> <p>改变前的值:{{myobject_oldValue}}</p> <p>改变后的值:{{myobject_currentValue}}</p> </div> </div> <div> <h5>{{watch_object}}</h5> <div> <!-- 当单观察数据myobject为对象时,如果键值发生变化, 为了监听到数据变化,需要添加deep:true参数 --> <p><input type="text" v-model="myobject.age"/></p> </div> <div> <p>改变前的值:{{myobject2_oldValue}}</p> <p>改变后的值:{{myobject2_currentValue}}</p> </div> </div> <hr> <hr> <div> <h5>主动调用$watch方法来进行数据监测</h5> <div> <p><input type="text" v-model="scope"/></p> </div> <div> <p>改变前的值:{{scope_oldValue}}</p> <p>改变后的值:{{scope_currentValue}}</p> </div> </div> <div></div> </div> </div> <script src="https://cdn.bootcss.com/vue/2.5.8/vue.min.js"></script> <script type="text/javascript"> var _vm = new Vue({ data : { title : 'computed属性和watch属性的区别之一【基础】', watch_string : '监测字符串', watch_function : '监测方法', watch_object : '监测对象', mystring : '', mystring_oldValue : '', mystring_currentValue : '', mystring2 : '', mystring2_oldValue : '', mystring2_currentValue : '', myobject : { name : 'yuzhiboyouzhu', age : 18, sex : '男' }, myobject_oldValue : {}, myobject_currentValue : {}, myobject2_oldValue : {}, myobject2_currentValue : {}, scope : '', scope_oldValue : '', scope_currentValue : '' }, watch : { mystring : function (currentValue, oldValue) { _vm.mystring_oldValue = oldValue ; _vm.mystring_currentValue = currentValue ; console.log('当前值:', currentValue, ', 原来值:', oldValue) ; }, /** * 监测值也可以为methods的方法名 */ // mystring2 : _vm.test, // 报错:Cannot read property 'test' of undefined // mystring2 : '_vm.test', // 报错:Cannot read property 'call' of undefined mystring2 : 'test', myobject: { /** * * 注意:当观察的数据为对象或数组时, * currentValue和oldValue是相等的, * 因为这两个形参指向的是同一个数据对象, * 所以页面上输出的“myobject_oldValue”和“myobject_currentValue”值都一样 * */ handler : function(currentValue, oldValue){ _vm.myobject_oldValue = oldValue ; _vm.myobject_currentValue = currentValue ; console.log('当前值:', currentValue, ', 原来值:', oldValue) ; }, deep:true}, // myobject.age : function(currentValue, oldValue) { // 报错:Unexpected token . 'myobject.age' : function(currentValue, oldValue) { // 键路径必须加上引号 _vm.myobject2_oldValue = oldValue ; _vm.myobject2_currentValue = currentValue ; console.log('当前值:', currentValue, ', 原来值:', oldValue) ; } }, methods : { test : function(currentValue, oldValue) { _vm.mystring2_oldValue = oldValue ; _vm.mystring2_currentValue = currentValue ; console.log('当前值:', currentValue, ', 原来值:', oldValue) ; } } }).$mount('#app') ; // 主动调用$watch方法来进行数据监测 _vm.$watch('scope', function(currentValue, oldValue) { _vm.scope_oldValue = oldValue ; _vm.scope_currentValue = currentValue ; }) ; </script></body>
阅读全文
0 0
- computed属性和watch属性的区别之一【watch的基本用法】
- computed属性和watch属性的区别之二【computed的基本用法】
- computed属性和watch属性的区别之三【购物车之watch】
- computed属性和watch属性的区别之四【购物车之computed】
- Vue.js的computed和watch用法及区别
- vue中watch和computed属性作用及区别
- methods和computed和watch的联系和区别
- Vue filter computed watch 的用法
- vue2 中 computed 和 watch 的异同?
- vue中computed 和 watch的异同
- computed和watch的使用场景
- vue 计算属性computed和观察watch 和方法methods
- vue中computed和watch区别
- Vuejs中关于computed、methods、watch的区别
- vue.js2.0关于computed、methods、watch、created的区别
- Vuejs中关于computed、methods、watch的区别
- vue.js中的computed和methods和watch之间的区别
- vue开发:vue中computed 和 watch的异同
- Linux:vim basic use
- 第二次实验
- leetcode 355. Design Twitter
- 排序算法之选择排序(二)
- Android 基于BaseActivity封装
- computed属性和watch属性的区别之一【watch的基本用法】
- GitHub上最火的Android开源项目持续更新中.....
- 64bitUbuntu执行32位程序报no such file or directory
- 【费用流】double模板
- 什么星座更适合当程序猿
- 20 个 CSS 高级技巧汇总
- (五上)安装node-gyp并build——解决 "node.lib:fatal error LNK1127" 问题
- 保刚性图像变形算法-Siggraph 2004
- iOS 使用cocoapods+github托管代码(制作发布 cocoapods 库)