美文网首页
七、computed的使用 ------ 2020-05-07

七、computed的使用 ------ 2020-05-07

作者: 自己写了自己看 | 来源:发表于2020-05-17 20:59 被阅读0次

1、computed基本使用

const vm = new Vue({
        el: '#app',
        data: {
            firstName: '二狗',
            lastName: '刘',
            xx: '1'
        },
        methods: {
            fullName() {
                return this.firstName = this.lastName;
            }
        },
        computed: {
            fullName() {
                return this.firstName + this.lastName;
            }
        }
    })

2、computed的get和set方法

const vm = new Vue({
        el: '#app',
        data: {
            firstName: '二狗',
            lastName: '刘',
            xx: '1'
        },
        methods: {
            fullName() {
                return this.firstName = this.lastName;
            }
        },
        computed: {
            fullName() {
                return this.firstName + this.lastName;
            }
        }
    })

computed的特点

1、computed是基于 Object.defineProperty();
2、computed会产生缓存,如果依赖的数据不发生变化,不会重新计算;

computed和methods的区别


相关文章

网友评论

      本文标题:七、computed的使用 ------ 2020-05-07

      本文链接:https://www.haomeiwen.com/subject/xxyzghtx.html