vue(7)computed属性的使用

1.computed是一个vue组件的属性,和data和methos是一个层级,computed中的每一个属性都有一个set和get方法,set方法会在对computed的属性进行赋值时调用,get方法则是获取值的时候调用。

2.computed中的属性一般用于对data中的数据再进行处理后返回。

3.获取computed中的属性的时候会有缓存,并不会每次都去调用get方法,除非get方法中用到的data中的属性发生变化。

4.computed中的属性和data中的属性一样,可以绑定到页面中。

5.代码:

<template>
    <div>
       <h1 >{{str}}</h1>
    </div>
</template>

<script>
export default {
   name:"App",
   data:function(){
       return {
           str1:'hello',
           str2:' world',
           str3:''
            };
   },
   computed:{
       str:{
           set(val){
               this.str3=val;
           },
           get(){
               return this.str1+this.str2;
           }
       }
   },
   methods:{
       
   }
}
</script>

<style scoped>
</style>
6.页面:
posted @ 2021-04-24 18:11  maycpou  阅读(687)  评论(0编辑  收藏  举报