美文网首页
computed和methods的区别

computed和methods的区别

作者: smartfeng | 来源:发表于2019-08-28 20:45 被阅读0次

Vue中computed怎么理解?
computed 又称计算属性, 什么情况下会用到计算属性,我们直接上代码理解一下

//我们有个name,经过多次逻辑处理最终显示到页面中
//它不是一个简单的声明式逻辑,我么需要看一段时间才能理解name
<template>
    <div>
        {{name.split('').reverse().join('')}}
        <br>
        {{message}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                name: 'hello vue!'
            }
        },
        //所以我们使用computed,处理复杂逻辑,计算属性
        computed: {
            message() {
                return this.name.split('').reverse().join('');
            }
        }
    }
</script>
//使用methods,每次render都会重新计算
<template>
    <div>
        {{getMessage()}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                message: 'hello vue!'
            }
        },
        methods: {
            getMessage() {
                return this.message.split('').reverse().join('');
            },
            now() {
                return Date.now()
            }
        },
    }
</script>

<style lang="scss" scoped>

</style>
//使用computed是基于它们的依赖进行缓存的,也就是说this.message不改变,每次访问都会立即返回结果
<template>
    <div>
        {{getMessage}}
    </div>
</template>

<script>
    export default {
        data() {
            return {
                message: 'hello vue!'
            }
        },
        computed: {
            getMessage() {
                return this.message.split('').reverse().join('');
            },
            now() {
                return Date.now()
            }
        },
    }
</script>

<style lang="scss" scoped>

</style>

computed 跟data相比更适用于复杂逻辑运算
computed 跟methods相比更适用于需要缓存的计算

相关文章

  • 5.计算属性

    计算属性关键词:computed methods方法和computed的区别methods和computed的区别...

  • 计算属性

    计算属性关键词:computed methods方法和computed的区别 区别: 可以使用 methods 来...

  • 05 methods和computed区别

    computed和methods的区别 在new Vue的配置参数中的computed和methods都可以处理大...

  • Day 07计算属性computed的使用

    computed 和methods的区别我们可以使用 methods 来替代 computed,效果上两个都是一样...

  • vue中methods 和 computed 和 watch方法

    methods 和 computed 和 watch方法的区别 computed是计算属性,是有依赖缓存的,只有在...

  • 11 计算属性和过滤器

    1.methods和computed的区别 如以下代码:computed1.html

  • 计算属性和过滤器

    1.methods和computed的区别 如以下代码:computed1.html

  • 计算属性

    1.计算属性get方法: 计算属性(computed)和Methods区别:计算属性(computed)适合:有缓...

  • 04.conputed

    methods和conputed区别 1.computed是属性调用,而methods是函数调用2.compute...

  • 关于Vue的一些入门知识

    watch 和 computed 和 methods 区别是什么?翻译一遍,说出作用,再找不同computed:计...

网友评论

      本文标题:computed和methods的区别

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