标签:this com 改变 多次 names new blog sage get
<div id="example"> {{ message.split(‘‘).reverse().join(‘‘) }} </div>
<div id="app"> 你好{{ ComMessage }} <p>{{ ComMsg() }}</p> </div> var vm = new Vue({ el: ‘#app‘, data: { message: ‘hello world‘ }, computed: { ComMessage: function(){ return this.message.split(‘‘).reverse().join(‘‘); //this 指向vm实例 } }, method: { ComMsg: function(){ return this.message.split(‘‘).reverse().join(‘‘); //this 指向vm实例 } } });
message
还没有发生改变,多次访问 reversedMessage
计算属性会立即返回之前的计算结 果,而不必再次执行函数。//意味着下面的计算属性将不再更新,因为 Date.now() 不是响应式依赖: computed: { now: function () { return Date.now() } }
<div id="demo">{{ fullName }}</div> ====================================================== var vm = new Vue({ el: ‘#demo‘, data: { firstName: ‘Foo‘, lastName: ‘Bar‘, fullName: ‘Foo Bar‘ }, watch: { firstName: function(val) { this.fullName = val + ‘ ‘ + this.lastName }, lastName: function(val) { this.fullName = this.firstName + ‘ ‘ + val } } }) ======================================================= var vm = new Vue({ el: ‘#demo‘, data: { firstName: ‘Foo‘, lastName: ‘Bar‘ }, computed: { fullName: function() { return this.firstName + ‘ ‘ + this.lastName } } })
// ... computed: { fullName: { // getter get: function() { return this.firstName + ‘ ‘ + this.lastName }, // setter set: function(newValue) { var names = newValue.split(‘ ‘) this.firstName = names[0] this.lastName = names[names.length - 1] } } } // ...
标签:this com 改变 多次 names new blog sage get
原文地址:http://www.cnblogs.com/hihao/p/7359275.html