computed
作用:动态数据依赖(当前数据依赖另一个数据)
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>侦听器watch</title></head><body><div id='app'>{{reverseMsg}}<h3>{{fullName}}</h3><button @click='handleClick'>改变</button></div><script src="./vue.js"></script><script>new Vue({el: '#app',data: {msg: 'hello world',firstName: 'ecithy',lastName: '哥'},methods: {handleClick(){this.msg = '计算属性computed';this.lastName = '妹';}},computed: {// computed默认只有getter方法// 计算属性最大的优点:产生缓存 如果数据没有发生变化 直接从缓存中取reverseMsg: function () {return this.msg.split('').reverse().join('')},fullName: function () {return this.firstName + this.lastName;}},})</script></body></html>
setter方法
输入的数据动态绑定给data
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>计算属性setter</title></head><body><div id='app'>{{content}}<input type="text" v-model='content' @input = 'handleInput'><button @click='handleClick'>获取</button></div><script src="./vue.js"></script><script>new Vue({el: '#app',data: {msg: '',},methods: {handleInput:function(event){const {value} = event.target;this.content = value;},handleClick(){// console.log();if (this.content) {console.log(this.content);}}},computed: {content:{set:function(newV){this.msg = newV;},get:function(){return this.msg;}}},})</script></body></html>
