Vue中的watch监视属性怎么应用

其他教程   发布日期:2025年04月06日   浏览次数:88

这篇“Vue中的watch监视属性怎么应用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue中的watch监视属性怎么应用”文章吧。

    一、监视属性watch

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

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

    3.监视的两种写法

    (1)new Vue时传入watch配置

    (2)通过vm.$watch监视

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>天气案例_监视属性</title>
    6. <!-- 引入Vue -->
    7. <script type="text/javascript" src="../js/vue.js"></script>
    8. </head>
    9. <body>
    10. <!--
    11. 监视属性watch:
    12. 1.当被监视的属性变化时, 回调函数自动调用, 进行相关操作
    13. 2.监视的属性必须存在,才能进行监视!!
    14. 3.监视的两种写法:
    15. (1).new Vue时传入watch配置
    16. (2).通过vm.$watch监视
    17. -->
    18. <!-- 准备好一个容器-->
    19. <div id="root">
    20. <h3>今天天气很{{info}}</h3>
    21. <button @click="changeWeather">切换天气</button>
    22. </div>
    23. </body>
    24. <script type="text/javascript">
    25. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
    26. const vm = new Vue({
    27. el:'#root',
    28. data:{
    29. isHot:true,
    30. },
    31. computed:{
    32. info(){
    33. return this.isHot ? '炎热' : '凉爽'
    34. }
    35. },
    36. methods: {
    37. changeWeather(){
    38. this.isHot = !this.isHot
    39. }
    40. },
    41. /* watch:{
    42. isHot:{
    43. immediate:true, //初始化时让handler调用一下
    44. //handler什么时候调用?当isHot发生改变时。
    45. handler(newValue,oldValue){
    46. console.log('isHot被修改了',newValue,oldValue)
    47. }
    48. }
    49. } */
    50. })
    51. vm.$watch('isHot',{
    52. immediate:true, //初始化时让handler调用一下
    53. //handler什么时候调用?当isHot发生改变时。
    54. handler(newValue,oldValue){
    55. console.log('isHot被修改了',newValue,oldValue)
    56. }
    57. })
    58. </script>
    59. </html>

    二、深度监视

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

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

    备注:

    • vue自身可以监视对象内部值的改变,但vue提供的watch默认不可以

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

    天气案例:

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>天气案例_深度监视</title>
    6. <!-- 引入Vue -->
    7. <script type="text/javascript" src="../js/vue.js"></script>
    8. </head>
    9. <body>
    10. <!--
    11. 深度监视:
    12. (1).Vue中的watch默认不监测对象内部值的改变(一层)。
    13. (2).配置deep:true可以监测对象内部值改变(多层)。
    14. 备注:
    15. (1).Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以!
    16. (2).使用watch时根据数据的具体结构,决定是否采用深度监视。
    17. -->
    18. <!-- 准备好一个容器-->
    19. <div id="root">
    20. <h3>今天天气很{{info}}</h3>
    21. <button @click="changeWeather">切换天气</button>
    22. <hr/>
    23. <h4>a的值是:{{numbers.a}}</h4>
    24. <button @click="numbers.a++">点我让a+1</button>
    25. <h4>b的值是:{{numbers.b}}</h4>
    26. <button @click="numbers.b++">点我让b+1</button>
    27. <button @click="numbers = {a:666,b:888}">彻底替换掉numbers</button>
    28. {{numbers.c.d.e}}
    29. </div>
    30. </body>
    31. <script type="text/javascript">
    32. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
    33. const vm = new Vue({
    34. el:'#root',
    35. data:{
    36. isHot:true,
    37. numbers:{
    38. a:1,
    39. b:1,
    40. c:{
    41. d:{
    42. e:100
    43. }
    44. }
    45. }
    46. },
    47. computed:{
    48. info(){
    49. return this.isHot ? '炎热' : '凉爽'
    50. }
    51. },
    52. methods: {
    53. changeWeather(){
    54. this.isHot = !this.isHot
    55. }
    56. },
    57. watch:{
    58. isHot:{
    59. // immediate:true, //初始化时让handler调用一下
    60. //handler什么时候调用?当isHot发生改变时。
    61. handler(newValue,oldValue){
    62. console.log('isHot被修改了',newValue,oldValue)
    63. }
    64. },
    65. //监视多级结构中某个属性的变化
    66. /* 'numbers.a':{
    67. handler(){
    68. console.log('a被改变了')
    69. }
    70. } */
    71. //监视多级结构中所有属性的变化
    72. numbers:{
    73. deep:true,
    74. handler(){
    75. console.log('numbers改变了')
    76. }
    77. }
    78. }
    79. })
    80. </script>
    81. </html>

    三、监视属性简写

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>天气案例_监视属性_简写</title>
    6. <!-- 引入Vue -->
    7. <script type="text/javascript" src="../js/vue.js"></script>
    8. </head>
    9. <body>
    10. <!-- 准备好一个容器-->
    11. <div id="root">
    12. <h3>今天天气很{{info}}</h3>
    13. <button @click="changeWeather">切换天气</button>
    14. </div>
    15. </body>
    16. <script type="text/javascript">
    17. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
    18. const vm = new Vue({
    19. el:'#root',
    20. data:{
    21. isHot:true,
    22. },
    23. computed:{
    24. info(){
    25. return this.isHot ? '炎热' : '凉爽'
    26. }
    27. },
    28. methods: {
    29. changeWeather(){
    30. this.isHot = !this.isHot
    31. }
    32. },
    33. watch:{
    34. //正常写法
    35. /* isHot:{
    36. // immediate:true, //初始化时让handler调用一下
    37. // deep:true,//深度监视
    38. handler(newValue,oldValue){
    39. console.log('isHot被修改了',newValue,oldValue)
    40. }
    41. }, */
    42. //简写
    43. /* isHot(newValue,oldValue){
    44. console.log('isHot被修改了',newValue,oldValue,this)
    45. } */
    46. }
    47. })
    48. //正常写法
    49. /* vm.$watch('isHot',{
    50. immediate:true, //初始化时让handler调用一下
    51. deep:true,//深度监视
    52. handler(newValue,oldValue){
    53. console.log('isHot被修改了',newValue,oldValue)
    54. }
    55. }) */
    56. //简写
    57. /* vm.$watch('isHot',(newValue,oldValue)=>{
    58. console.log('isHot被修改了',newValue,oldValue,this)
    59. }) */
    60. </script>
    61. </html>

    以上就是Vue中的watch监视属性怎么应用的详细内容,更多关于Vue中的watch监视属性怎么应用的资料请关注九品源码其它相关文章!