<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>01-计算属性的基本使用</title>
</head><body><div id="app"><h2>{{firstName + ' ' + lastName}}</h2><h2>{{firstName}} {{lastName}}</h2><h2>{{getFullName()}}</h2></div><script src="../js/vue.js"></script><script>const app = new Vue({el: '#app',data: {firstName: 'Lebron',lastName: 'James'},methods: {getFullName() {return this.firstName + ' ' + this.lastName;}}})</script>
</body></html>
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>01-计算属性的基本使用</title>
</head><body><div id="app"><h2>{{firstName + ' ' + lastName}}</h2><h2>{{firstName}} {{lastName}}</h2><h2>{{getFullName()}}</h2><h2>{{fullName}}</h2></div><script src="../js/vue.js"></script><script>const app = new Vue({el: '#app',data: {firstName: 'Lebron',lastName: 'James'},// computed:计算属性()computed: {fullName: function() {return this.firstName + ' ' + this.lastName;}},methods: {getFullName() {return this.firstName + ' ' + this.lastName;}}})</script>
</body></html>
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>02-计算属性的复杂操作</title>
</head><body><div id="app"><h2>总价格:{{totalPrice}}</h2></div><script src="../js/vue.js"></script><script>const app = new Vue({el: '#app',data: {books: [{id: 110,name: '深入理解计算机原理',price: 119}, {id: 111,name: '斗罗大陆之绝世唐门',price: 643}, {id: 112,name: 'JavaScript从入门到精通',price: 99}, {id: 113,name: '高等数学',price: 37}]},computed: {totalPrice: function() {let result = 0;for (let i = 0; i < this.books.length; i++) {result += this.books[i].price;}return result;}}})</script>
</body></html>
computed: {fullName: function() {return this.firstName + ' ' + this.lastName;}
}
fullName: {// 一般情况set都没有,只有getset: function() {},get: function() {return this.firstName + ' ' + this.lastName;}
}
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>04-计算属性和methods的对比</title>
</head><body><div id="app"><!-- 1.直接拼接,过于繁琐 --><h2>{{firstName}} {{lastName}}</h2><!-- 2.通过定义methods --><h2>{{getFullName()}}</h2><h2>{{getFullName()}}</h2><h2>{{getFullName()}}</h2><h2>{{getFullName()}}</h2><h2>{{getFullName()}}</h2><!-- 3.通过定义computed --><h2>{{fullName}}</h2><h2>{{fullName}}</h2><h2>{{fullName}}</h2><h2>{{fullName}}</h2><h2>{{fullName}}</h2><h2>{{fullName}}</h2></div><script src="../js/vue.js"></script><script>const app = new Vue({el: '#app',data: {firstName: 'Lebron',lastName: 'James'},// computed:计算属性()computed: {fullName: function() {console.log('通过computed');return this.firstName + ' ' + this.lastName;}},methods: {getFullName() {console.log('通过methods');return this.firstName + ' ' + this.lastName;}}})</script>
</body></html>
相比之下,每当触发重新渲染时,调用方法将总会再次执行函数。
我们为什么需要缓存?假设我们有一个性能开销比较大的计算属性 A,它需要遍历一个巨大的数组并做大量的计算。然后我们可能有其他的计算属性依赖于 A。如果没有缓存,我们将不可避免的多次执行 A 的 getter!如果你不希望有缓存,请用方法来替代。
本文发布于:2024-01-30 21:11:47,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170662031022872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |