首页 > 其他分享 >Vue2入门之超详细教程九-监视属性

Vue2入门之超详细教程九-监视属性

时间:2023-05-07 16:11:22浏览次数:49  
标签:教程 Vue 之超 watch isHot numbers Vue2 true newValue

1、简介

  监视属性watch:

    1.当被监视的属性变化时,回调函数自动调用,进行相关操作

    2.监视的属性必须存在,才能进行监视!!

    3.监视的两种写法:

      (1) new Vue时传入watch配置

      (2) 通过vm.$watch监视

  深度监测:

    (1) Vue中的watch默认不监测对象内部值的改变(一层)

    (2) 配置deep:true可以监测对象内部值改变(多层)

  备注:

    (1) Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以

    (2) 使用watch时根据数据的具体结构,决定是否采用深度监视

  学习Vue之前最后会一些HTML和CSS的基础知识,HTML基础知识 传送门,CSS基础知识 传送门

2、监视属性

1. 天气案例

  在vscode中创一个新目录,叫“08_监视属性”,在下面创建一个“1_计算属性方式实现.html”文件,在里面输入以下代码:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        new Vue({

            el:'#root',

            data:{

                isHot:true

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            }

        })

    </script>

</body>

</html>

  该案例,当点击按钮时,会调用changeWeather方法,赋值isHot为isHot的取反的值

2. 监视属性

第一种写法

  可以监视Vue中的属性,当发生变化时会被调用

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        new Vue({

            el:'#root',

            data:{

                isHot:true

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

            watch:{

                isHot:{

                    //初始化时立即调用一下

                    // immediate:true,

                    //handler什么时候调用?当IsHot发生改变时被调用,可以接收两个值,第一个为修改后,第二个为修改前

                    handler(newValue,oldValue){

                        console.log('isHot被修改该了',newValue,oldValue)

                    }

                }

            }

       

        })

    </script>

</body>

</html>

   使用场景:比如当气温低于15度是时,提醒多穿衣服:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        new Vue({

            el:'#root',

            data:{

                isHot:true,

                temperature:20

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                    this.temperature--

                }

            },

            watch:{

                temperature:{

                    //初始化时立即调用一下

                    // immediate:true,

                    //handler什么时候调用?当IsHot发生改变时被调用,可以接收两个值,第一个为修改后,第二个为修改前

                    handler(newValue,oldValue){

                        console.log('isHot被修改该了',newValue,oldValue)

                        if(newValue < 15){

                            alert('天冷啦,多穿衣服')

                        }

                       

                    }

                }

            }

       

        })

    </script>

</body>

</html>

 第二种写法

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        const vm = new Vue({

            el:'#root',

            data:{

                isHot:true

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

       

        })

        vm.$watch('isHot',{

            handler(newValue,oldValue){

                console.log('isHot被修改该了',newValue,oldValue)

            }

        })

    </script>

</body>

</html>

 3. 深度监视

  以上监测都是针对一级属性监测,当存在多级属性时如何进行检测呢?

多级监测

  比如:numbers属性下存在两个属性a和b,我们只针对a变动做检测

  再使用以上方式就不可以了,这会必须使用’numbers.a’这种方式,注意这里不能直接简写为numbers.a必须用引号引起来,完整代码如下:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

        <hr>

        <h3>a的值是{{numbers.a}}</h3>

        <button @click="numbers.a++">点我让a+1</button>

 

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        new Vue({

            el:'#root',

            data:{

                isHot:true,

                numbers:{

                    a:1,

                    b:1

                }

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

            watch:{

                'numbers.a':{

                    //handler什么时候调用?当IsHot发生改变时被调用,可以接收两个值,第一个为修改后,第二个为修改前

                    handler(newValue,oldValue){

                        console.log('a被改变了',newValue,oldValue)

                    }

                }

            }

       

        })

    </script>

</body>

</html>

深度监测

  以上面代码为例,如果想a或者b任意一个发生变化时都可以监测到,你可能想可以这样写:

            

        watch:{

                'numbers':{

                    handler(newValue,oldValue){

                        console.log('numbers被改变了',newValue,oldValue)

                    }

                }

            }

  明确告诉大家,这样是不可以的,如下图,这样写只会监测粉色线中的部分,并不会监测绿色内的部分

  像这种方式我们只需要加一个配置即可:

  deep:true,

  完整代码如下:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

        <hr>

        <h3>a的值是{{numbers.a}}</h3>

        <button @click="numbers.a++">点我让a+1</button>

        <hr>

        <h3>b的值是{{numbers.b}}</h3>

        <button @click="numbers.b++">点我让b+1</button>

        <button @click="numbers={a:111,b:222}">彻底替换掉numbers</button>

 

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        new Vue({

            el:'#root',

            data:{

                isHot:true,

                numbers:{

                    a:1,

                    b:1

                }

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

            watch:{

                'numbers':{

                    deep:true,

                    handler(newValue,oldValue){

                        console.log('numbers被改变了',this.numbers.a,this.numbers.b)

                    }

                }

            }

       

        })

    </script>

</body>

</html>

4. 监测属性的简写形式

Vue中简写形式

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        new Vue({

            el:'#root',

            data:{

                isHot:true,

                numbers:{

                    a:1,

                    b:1

                }

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

            watch:{

                //正常写法

                // 'isHot':{

                //     //immediate:true, //初始化时让handler调用一下

                //     //deep:true, //深度监视

                //     handler(newValue,oldValue){

                //         console.log('isHot被改变了',this.numbers.a,this.numbers.b)

                //     }

                // }

                //简写形式

                isHot(){

                    console.log('isHot被改变了',this.numbers.a,this.numbers.b)

                }

            }

       

        })

    </script>

</body>

</html>

vm.$watch中简写形式

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        const vm = new Vue({

            el:'#root',

            data:{

                isHot:true,

                numbers:{

                    a:1,

                    b:1

                }

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

       

        })

        //正常写法

        // vm.$watch('isHot',{

        //     immediate:true,

        //     deep:true,

        //     handler(newValue,oldValue){

        //         console.log('isHot被修改了',newValue,oldValue)

        //     }

        // })

        //简写方式

        vm.$watch('isHot',function(newValue,oldValue){

            console.log('isHot被修改了',newValue,oldValue)

        })

    </script>

</body>

</html>

3、测试

  我们来检测一个不存在的属性’123’来看看效果

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="../js/development/vue.js"></script>

</head>

<body>

    <div id="root">

        <h2>今天天气很{{info}}</h2>

        <button @click="changeWeather">切换天气</button>

    </div>

    <script type="text/javascript">

        Vue.config.douctionTip = false

        const vm = new Vue({

            el:'#root',

            data:{

                isHot:true

            },

            computed:{

                info(){

                    return this.isHot ? '炎热':'凉爽'

                }

            },

            methods:{

                changeWeather(){

                    this.isHot = !this.isHot

                }

            },

       

        })

        vm.$watch('123',{

            handler(newValue,oldValue){

                console.log('isHot被修改该了',newValue,oldValue)

            }

        })

    </script>

</body>

</html>

  以上代码的效果为,正常执行,也不会报错,但不会监视到内容,所以也不会执行监视内部的代码

4、小结

  如果只要要监视的属性,可以直接在new Vue中写

  不确定要监视的属性,可以使用vm.$watch()方式

  多级监测属性时,不能使用简写方式,必须用引号引起来

  深度监测属性时,需要加一个配置:deep:true

  当需要使用immediate或deep等配置是,不能使用监测属性的简写形式

标签:教程,Vue,之超,watch,isHot,numbers,Vue2,true,newValue
From: https://www.cnblogs.com/lirongyang/p/17369825.html

相关文章

  • vue2使用图片懒加载之vue-lazyload
    1.为什么要进行图片懒加载呢?使用图片懒加载的主要原因是为了优化网站或应用程序的性能和用户体验。当页面中存在大量图片时,如果一次性全部加载会影响页面的加载速度,用户可能需要等待很长时间才能看到完整的页面内容,这会影响用户的体验和满意度。2.如何实现图片懒加载使用http......
  • windows安装zincsearch教程
    1.首先去github下载安装包   下载最新的ZincSearch二进制文件  选择版本 zincsearch_0.4.5_Windows_x86_64.tar.gz  然后解压压缩包,   2.添加环境变量ZINC_FIRST_ADMIN_USER=adminZINC_FIRST_ADMIN_PASSWORD=123456账号和密码可以随意设置,不过为了简单......
  • 最新fl studio 21.0.3.351中文版功能介绍/下载安装/语言切换/激活解锁教程
    最新水果软件flstudio21.0.3.3517中文版是一款免费的音乐编曲制作软件,有了它你可以制作出色的音乐。它为您提供了一个集成的开发环境,使用起来非常简单有效,您的工作会变得更有条理。同时FLStudio为用户提供了更先进和原创的音乐制作理念,用户可以轻松地混合、编排和创作多种歌曲。......
  • AutoGPT:有手就会的安装教程
    AutoGPT是什么Auto-GPT是一个实验性开源应用程序,展示了GPT-4语言模型的功能。该程序由GPT-4驱动,将LLM的“思想”链接在一起,以自主实现您设定的任何目标。作为GPT-4完全自主运行的首批示例之一,Auto-GPT突破了AI的可能性界限。AutoGPT是github上最火的GPT项目,......
  • Three.js教程:访问几何体对象的数据
    推荐:将NSDT场景编辑器加入你的3D工具链其他系列工具:NSDT简石数字孪生访问几何体对象的数据实际开发项目的时候,可能会加载外部模型,有些时候需要获取模型几何体的顶点数据,如果想获取几何体的顶点数据首先要熟悉three.js几何体BoxGeometry和BufferGeometry的结构。访问几何体顶点......
  • iBatis简单入门教程
       iBatis简单入门教程 iBatis简介:iBatis是apache的一个开源项目,一个O/RMapping解决方案,iBatis最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis已经改名为Mybatis了。官网为:ht......
  • UEFI 基础教程 (三) - 运行第一个PEI Driver
    一、编写源代码编写C:\edkii\OvmfPkg\MyHelloWorldPEIMDriver\MyHelloWorldPEIMDriver.c#include<uefi.h>#include<Library/UefiLib.h>#include<Library/BaseLib.h>#include<Library/DebugLib.h>#include<Library/BaseMemoryLib.h>......
  • GPT4free安装部署教程 - 白嫖GPT
    前言为啥之前一直没有更新GPT相关的内容,因为个人觉得如果每次都需要使用付费使用API的话,那这个工具还是很难在个人手上被运用起来,多测试几次关键字和清洗数据,API的费用对个人来说都太高昂了直到GPT4free出现公众号后台回复1002,获取GPT试用网址部署使用直接开始部署吧,别像其他文......
  • 俩小伙一晚上写了个 AI 应用,月入两万??(文末附开发教程)
    开发出一款能够与AI对话生成和编辑思维导图的工具,听起来似乎只能是一群专业的AI背景团队花费大量的时间和精力训练模型,打磨应用才能完成的事情。但是,两名大学生却在一夜之间完成了,就像炼金术士将庸俗的材料转化成黄金一样,他们将代码转化为了神奇的工具,下面我们来一起揭开这个......
  • Unix教程_编程入门自学教程_菜鸟教程-免费教程分享
    教程简介UNIX/Linux操作系统(OS)入门教程-从基本概念开始,简单易学地了解UNIX的基础知识,包括入门,UnixKorn和BourneShell和编程,文件权限/访问模式,环境,实用程序,管道和过滤器,网络通信实用程序,文件系统,目录,内存管理,特殊变量,vi编辑器,什么是Shell?,使用Shell变量,数组,基本运算符,决策,循......