• 【Vue配置项】 computed计算属性 | watch侦听属性


    目录

    前言

    computed计算属性

    什么是计算属性?

    Vue的原有属性是什么?

    得到的全新的属性是什么?

    计算属性怎么用?

    计算属性的作用是什么?

    为什么说代码执行率高了?

    computed计算属性中的this指向

    computed计算属性简写

    watch侦听属性

    语法格式

    watch侦听属性中的this指向

    watch深度监视

    watch的简写

    watch和computed如何选择

    异步情况下

    computed

    watch

    watch中异步中箭头函数和普通函数this指向


    前言

    继上篇文章介绍了Vue配置项中的methods,本文继续介绍Vue配置项中的computed计算属性和watch侦听属性以及在使用如何选择

    computed计算属性

    什么是计算属性?

    使用Vue的原有属性,经过一系列的计算,最终得到了一个全新的属性,叫做计算属性。

    Vue的原有属性是什么?

    data对象当中的属性可以叫做Vue的原有属性。

    得到的全新的属性是什么?

    表示生成了一个新的属性,和data中的属性无关了,新的属性也有自己的属性名和属性值。

    计算属性怎么用?

    语法格式:需要一个新的配置项 computed

    computed:{

            计算属性1:{

                    get(){

                            当读取计算属性1时,getter方法被自动调用

                    },

                    set(){

                            当修改计算属性1时,setter方法被自动调用

                    }

            },

            计算属性2:{}

    }

    计算属性的作用是什么?

    1. 代码得到了复用
    2. 代码变得更加容易维护
    3. 代码的执行效率高了

    为什么说代码执行率高了?

    其实在methods中也可以调用函数方法来完成computed计算属性能完成的事,那为什么会引入computed计算属性呢?

    举例

    1. class="app">
    2. <h1>{{msg}}h1>
    3. <input type="text" v-model="info">
    4. {{hh()}}
    5. {{hh()}}
    6. {{hh()}}
    7. {{hh()}}
    8. {{hh()}}
    9. {{xx}}
    10. {{xx}}
    11. {{xx}}
    12. {{xx}}
    13. {{xx}}
    14. div>
    15. <script>
    16. const vm = new Vue({
    17. el:'.app',
    18. data:{
    19. msg:'computed计算属性',
    20. info:''
    21. },
    22. methods:{
    23. hh(){
    24. console.log('methods方法执行了');
    25. return 'hh'
    26. }
    27. },
    28. computed:{
    29. xx:{
    30. get(){
    31. console.log('计算属性执行了');
    32. return 'xx'
    33. }
    34. }
    35. }
    36. })
    37. script>

    执行此代码,methods中的函数方法以及computed中的计算属性各调用5次

    由结果看出,methods中的方法重复执行了5次,而computed计算属性只执行了一次。

    这是因为computed计算属性遵循一个缓存机制,将重复存入浏览器中,使用时直接拿出来即可,这样代码的执行效率就变高了

    computed计算属性中的this指向

    methods中的this是指向vue实例的,那computed计算属性中的this是指向什么呢?

    1. class="app">
    2. <h1>{{msg}}h1>
    3. <input type="text" v-model="info">
    4. {{reverse}}
    5. div>
    6. <script>
    7. const vm = new Vue({
    8. el:'.app',
    9. data:{
    10. msg:'computed计算属性',
    11. info:''
    12. },
    13. computed:{
    14. reverse:{
    15. get(){
    16. console.log(this);
    17. }
    18. }
    19. }
    20. })
    21. script>

    可知,computed计算属性中的this是指向Vue实例对象的

    computed计算属性简写

    当不使用set()方法时,仅使用get()方法时可以使用简写

    例:反转字符串

    1. class="app">
    2. <h1>{{msg}}h1>
    3. <input type="text" v-model="info">
    4. 字符串反转后 :{{reverse}}
    5. div>
    6. <script>
    7. const vm = new Vue({
    8. el:'.app',
    9. data:{
    10. msg:'computed计算属性',
    11. info:''
    12. },
    13. computed:{
    14. reverse(){
    15. return this.info.split('').reverse().join('')
    16. }
    17. }
    18. })
    19. script>

    直接省略get,将:换成()后接{}即可,简写和完整写法对比

    1. // 简写
    2. reverse(){
    3. return this.info.split('').reverse().join('')
    4. },
    5. // 完整写法
    6. reverse:{
    7. get(){
    8. return this.info.split('').reverse().join('')
    9. }
    10. }

    注意,简写后和methods中的函数方法写法相同,但这不是函数方法,这是计算属性

    watch侦听属性

    侦听属性变化

    语法格式

    1. watch:{
    2. //可以监视多个属性
    3. //监视哪个属性,直接写属性名字即可
    4. //可以监视Vue的原有属性,也可以监视计算属性
    5. num:{
    6. //固定写法,方法名必须叫handler
    7. //当被监视的属性发生变化时,handler就会自动调用一次
    8. //handler方法有两个参数,前一个参数代表属性值改变后的新值,后一个参数代表属性值改变之前的旧值
    9. handler(newValue,oldValue){
    10. console.log(newValue,oldValue);
    11. }
    12. }
    13. }

    watch侦听属性中的this指向

    1. watch:{
    2. //可以监视多个属性
    3. //监视哪个属性,直接写属性名字即可
    4. //可以监视Vue的原有属性,也可以监视计算属性
    5. num:{
    6. //固定写法,方法名必须叫handler
    7. //当被监视的属性发生变化时,handler就会自动调用一次
    8. //handler方法有两个参数,前一个参数代表属性值改变后的新值,后一个参数代表属性值改变之前的旧值
    9. handler(newValue,oldValue){
    10. console.log(this === vm);
    11. }
    12. }
    13. }

    由此可知,watch中的this也是指向vue实例对象的

    watch深度监视

    如何侦听对象呢?

    例:侦听a对象里面的b

    1. class="app">
    2. <h1>{{msg}}h1>
    3. <input type="text" v-model="a.b">
    4. div>
    5. <script>
    6. const vm = new Vue({
    7. el:'.app',
    8. data:{
    9. msg:'watch侦听属性',
    10. a:{
    11. b:0
    12. }
    13. },
    14. watch:{
    15. //可以监视多个属性
    16. //监视哪个属性,直接写属性名字即可
    17. //可以监视Vue的原有属性,也可以监视计算属性
    18. 'a.b':{
    19. //固定写法,方法名必须叫handler
    20. //当被监视的属性发生变化时,handler就会自动调用一次
    21. //handler方法有两个参数,前一个参数代表属性值改变后的新值,后一个参数代表属性值改变之前的旧值
    22. handler(newValue,oldValue){
    23. alert('111')
    24. }
    25. }
    26. }
    27. })
    28. script>

    侦听这种嵌套的需要加上" ",原本就是有的,只是省略了,当侦听这种嵌套关系时,需要加上" ".

    如果更深层次的嵌套,是否需要一直“...”下去呢?

    不需要,vue给我提供了deep属性

    deep:true(默认是false),当deep:true时,代表开启了深度监视,只需要监视对象便可监听该对象内的所有属性

    1. watch:{
    2. //可以监视多个属性
    3. //监视哪个属性,直接写属性名字即可
    4. //可以监视Vue的原有属性,也可以监视计算属性
    5. a:{
    6. deep:true,
    7. //固定写法,方法名必须叫handler
    8. //当被监视的属性发生变化时,handler就会自动调用一次
    9. //handler方法有两个参数,前一个参数代表属性值改变后的新值,后一个参数代表属性值改变之前的旧值
    10. handler(newValue,oldValue){
    11. alert('111')
    12. }
    13. }
    14. }

    watch的简写

    原:

    1. watch:{
    2. a:{
    3. handler(){
    4. console.log('监听到了');
    5. }
    6. }
    7. }

    简写:

    简写的条件是,不使用深度监视及其他的任何属性

    1. watch:{
    2. a(){
    3. console.log('监听到了');
    4. }
    5. }

    watch和computed如何选择

    • 当computed和watch都能完成某个功能时,优先选择computed
    • 当程序中采用异步的方式时,只能使用watch

    例:比较大小

    先使用watch侦听属性

    1. class="app">
    2. <h1>{{msg}}h1>
    3. <button @click="add1">+1button>
    4. <button @click="add2">+1button>
    5. num1:{{num1}}
    6. num2:{{num2}}
    7. 比较结果:{{daxiao}}
  • <script>
  • const vm = new Vue({
  • el:'.app',
  • data:{
  • msg:'computed和watch的选择',
  • num1:1,
  • num2:1
  • daxiao:""
  • },
  • methods:{
  • add1(){
  • return this.num1++
  • },
  • add2(){
  • return this.num2++
  • }
  • },
  • watch:{
  • num1(){
  • if(this.num1 == this.num2){
  • this.daxiao= this.num1 +'='+ this.num2
  • }else if(this.num1 > this.num2){
  • this.daxiao= this.num1+'>'+ this.num2
  • }else {
  • this.daxiao= this.num1+'<'+this.num2
  • }
  • },
  • num2(){
  • if(this.num1 == this.num2){
  • this.daxiao= this.num1 +'='+ this.num2
  • }else if(this.num1 > this.num2){
  • this.daxiao= this.num1+'>'+ this.num2
  • }else {
  • this.daxiao= this.num1+'<'+this.num2
  • }
  • },
  • }
  • })
  • script>
  • 可以完成该功能

    使用computed计算属性

    也能完成该功能,此种情况下选择computed计算属性

    异步情况下

    computed

    1. computed:{
    2. daxiao(){
    3. setTimeout(()=>{
    4. if(this.num1 == this.num2){
    5. return this.num1 +'='+ this.num2
    6. }else if(this.num1 > this.num2){
    7. return this.num1+'>'+ this.num2
    8. }else {
    9. return this.num1+'<'+this.num2
    10. }
    11. },3000)
    12. }
    13. },

    无法完成比较大小的功能

    这是因为,在异步情况下的箭头函数由谁调用,this就指向谁,这里的是由javascript引擎调用的,return的时候也是把值返回给javascript引擎

    watch

    1. watch:{
    2. num1(){
    3. setTimeout(()=>{
    4. if(this.num1 == this.num2){
    5. this.daxiao= this.num1 +'='+ this.num2
    6. }else if(this.num1 > this.num2){
    7. this.daxiao= this.num1+'>'+ this.num2
    8. }else {
    9. this.daxiao= this.num1+'<'+this.num2
    10. }
    11. },2000)
    12. },
    13. num2(){
    14. setTimeout(()=>{
    15. if(this.num1 == this.num2){
    16. this.daxiao= this.num1 +'='+ this.num2
    17. }else if(this.num1 > this.num2){
    18. this.daxiao= this.num1+'>'+ this.num2
    19. }else {
    20. this.daxiao= this.num1+'<'+this.num2
    21. }
    22. },2000)
    23. },
    24. }

    在异步情况下,watch可以完成该功能

    watch中异步中箭头函数和普通函数this指向

    分别在箭头函数以及普通函数中打印this

    1. watch:{
    2. num1(){
    3. setTimeout(()=>{
    4. console.log(this);
    5. if(this.num1 == this.num2){
    6. this.daxiao= this.num1 +'='+ this.num2
    7. }else if(this.num1 > this.num2){
    8. this.daxiao= this.num1+'>'+ this.num2
    9. }else {
    10. this.daxiao= this.num1+'<'+this.num2
    11. }
    12. },2000)
    13. },
    14. num2(){
    15. setTimeout(function(){
    16. console.log(this);
    17. if(this.num1 == this.num2){
    18. this.daxiao= this.num1 +'='+ this.num2
    19. }else if(this.num1 > this.num2){
    20. this.daxiao= this.num1+'>'+ this.num2
    21. }else {
    22. this.daxiao= this.num1+'<'+this.num2
    23. }
    24. },2000)
    25. },
    26. }

    可以看出,在箭头函数中,this是指向Vue实例的,反而普通函数中的this指向window

    在箭头函数中,this之所以指向Vue实例是因为,箭头函数是没有this,是继承过来的,那么在异步中,该函数是被Vue实例管理的num1调用的,所以this是指向Vue实例的

    在普通函数中,this指向调用者,settimeout异步是window调用的,所以this是指向window的

  • 相关阅读:
    【构建ML驱动的应用程序】第 3 章 :构建您的第一个端到端管道
    MaxKey单点登录认证系统v3.5.10GA发布
    PostgreSQL数据库动态共享内存管理器——dynamic shared memory segment
    基于PHP的线上购物商城,MySQL数据库,PHPstudy,原生PHP,前台用户+后台管理,完美运行,有一万五千字论文。
    【postgresql】CentOS7 安装Pgweb
    【爬虫笔记】Python爬虫简单运用爬取代理IP
    内网渗透之内网信息收集(三)
    中国居民身份证号码校验算法
    【算法100天 | 18】回文链表的多种解法(JAVA实现)
    软件配置 | Git下载、安装及卸载
  • 原文地址:https://blog.csdn.net/weixin_68854196/article/details/134452605