zoukankan      html  css  js  c++  java
  • 21.计算属性的setter和getter方法

    <style>
            .changeList {
                color: red;
            }
        </style>
    <body>
        <div id="app">
            <h1> {{ fullName }} </h1>
    
            <h2> {{ fullNameT }} </h2>
            <h2> {{ fullNameT }} </h2>
            <h2> {{ fullNameT }} </h2>
            <h2> {{ fullNameT }} </h2>
    
            <h2> {{ getFullName() }} </h2>
            <h2> {{ getFullName() }} </h2>
            <h2> {{ getFullName() }} </h2>
            <h2> {{ getFullName() }} </h2>
    
    
        </div>
        <script src='https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js'></script>
        <script>
            const app = new Vue({
                el: '#app',
                data: {
                    firstName: "李银河",
                    lastName: "你好呀"
                },
                computed: { // 计算属性里面有 set 和get 方法的 一般不用set方法来改变属性 只用 get方法只读属性
                    // get方法一般都是省略  直接像 fullName2一样写
                    fullName: {
                        // set: function(newValue) {
                        //     console.log(newValue);
                        //     const names = newValue.split(' ');
                        //     this.firstName = names[0];
                        //     this.lastName = names[1]
                        // },
                        get: function() {
                            return this.firstName + ' ' + this.lastName;
                        }
                    },
                    fullNameT: function() { // 用计算属性写 性能更高  因为它有缓存
                        console.log("fullNameT");
                        return this.firstName + ' ' + this.lastName;
    
                    }
    
                },
                methods: {
                    getFullName: function() {
                        console.log("getFullName"); // 打印4次  如果页面同时打印类似 上面的内容 
                        //  在methods属性  里面写和计算属性一样的内容 性能低
                        return this.lastName + " " + this.firstName
                    }
                }
            })
        </script>
    
    </body>
  • 相关阅读:
    Python列表推导式,字典推导式,元组推导式
    python装饰器@,内嵌函数,闭包
    7-route命令
    6-mv命令
    5-ln链接命令
    4-linux建立用户用户组以及新用户的赋权
    3-gzip压缩解压命令
    2-date命令
    1-cp命令
    UIViewContentMode的各种效果
  • 原文地址:https://www.cnblogs.com/yanglaxue/p/14205750.html
Copyright © 2011-2022 走看看