[VUE] calculated properties

Computed Property

Key attributes of computing prominent in 属性the word (attribute noun), first it is 属性followed by this attribute has 计算the ability (to calculate the verb), here 计算is a function; Simply put, it is a result of the calculation can be cached properties (the behavior converted into static properties), and nothing more; conceivable for the cache!

<div id="app">
    <p>时间戳:{{currenttime1()}}</p>
    <p>时间戳:{{currenttime2}}</p>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            message: "hello vue"
        },
        methods: {
            //在控制台中调用vm.currenttime1(),获得的时间是实时的
            currenttime1: function () {
                return Date.now();
            }
        },
        computed: {
            //在控制台中调用vm.currenttime2,数据是不会变的,类似于缓存
            currenttime2: function () {
                return Date.now();
            }
        }
    });
</script>

in conclusion:

When you call a method, every time need to be calculated, since there is bound to produce calculation overhead, that if the results are not always change it? Then you can consider this result to be cached, using computational properties can easily do this, the main characteristics of the property is to calculate the results will not change frequently cache in order to save our system overhead;

Guess you like

Origin www.cnblogs.com/pinked/p/12311419.html