Vue-监听属性

监听属性

简单监听

点击切换名字,来回变更Tom/Jerry,输出 你好,Tom/Jerry

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true
      },
      computed: {
        name(){
          return this.isTom ? "Tom" : "Jerry"
        }
      },
      methods: {
        changeName(){
          this.isTom = !this.isTom
        }
      },
      watch: {
        // isTom:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("isTom切换了", oldValue + "->" + newValue)
        //   }
        // },
        name:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("名字切换了", oldValue + "->" + newValue)
          }
        }
        
      },
    });
    vm.$watch('isTom',{
      immediate:true, //初始化就执行handler方法
      // isTom发生改变时执行
      handler(newValue,oldValue){
        console.log("isTom切换了", oldValue + "->" + newValue)
      }
    })
  </script>
</html>
  • 效果

深度监听

监听对象发生变更 numbers:{x:1,y:1}

对象某个属性监听 (x)

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
        <h2>单属性监听</h2>
        <button @click="numbers.x++">监听x+1</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true,
        numbers:{
          x:1,
          y:2
        }
      },
      computed: {
        name(){
          return this.isTom ? "Tom" : "Jerry"
        }
      },
      methods: {
        changeName(){
          this.isTom = !this.isTom
        }
      },
      watch: {
        // isTom:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("isTom切换了", oldValue + "->" + newValue)
        //   }
        // },
        name:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("名字切换了", oldValue + "->" + newValue)
          }
        },
        'numbers.x':{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("x变更了", oldValue + "->" + newValue)
          }
        },
        numbers:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("numbers变更了", oldValue + "->" + newValue)
          }
        }
        
      },
    });
    vm.$watch('isTom',{
      immediate:true, //初始化就执行handler方法
      // isTom发生改变时执行
      handler(newValue,oldValue){
        console.log("isTom切换了", oldValue + "->" + newValue)
      }
    })
  </script>
</html>
  • 效果

发现变更x, 并没有监听到numbers变更

属性变更能监听到整个对象的变化

深度监听 deep:true 可以监听对象内部的值改变(支持多层级)

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
        <h2>单属性监听</h2>
        <button @click="numbers.x++">监听x+1</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true,
        numbers:{
          x:1,
          y:2
        }
      },
      computed: {
        name(){
          return this.isTom ? "Tom" : "Jerry"
        }
      },
      methods: {
        changeName(){
          this.isTom = !this.isTom
        }
      },
      watch: {
        // isTom:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("isTom切换了", oldValue + "->" + newValue)
        //   }
        // },
        name:{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("名字切换了", oldValue + "->" + newValue)
          }
        },
        'numbers.x':{
          immediate:true, //初始化就执行handler方法
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("x变更了", oldValue + "->" + newValue)
          }
        },
        numbers:{
          immediate:true, //初始化就执行handler方法,
          deep:true,// 可以监听对象内部的值改变(支持多层级)
          // isTom发生改变时执行
          handler(newValue,oldValue){
            console.log("numbers变更了", JSON.stringify(oldValue) + "->" +  JSON.stringify(newValue))
          }
        }
        
      },
    });
    vm.$watch('isTom',{
      immediate:true, //初始化就执行handler方法
      // isTom发生改变时执行
      handler(newValue,oldValue){
        console.log("isTom切换了", oldValue + "->" + newValue)
      }
    })
  </script>
</html>
  • 效果

简写

前提条件:不需要特殊配置(deep、immediate)方可简写

  • 代码
javascript 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>监听属性</title>
    <!--  引入Vue  -->
    <script type="text/javascript" src="../js/vue.js"></script>
  </head>
  <body>
    <div id="root">
      <h1>监听属性</h1>
      <div>
        <h2>你好,{{name}}</h2>
        <button @click="changeName">切换名字</button>
      </div>
    </div>
  </body>
  <script type="text/javascript">
    Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示
    const vm = new Vue({
      el: "#root",
      data: {
        isTom: true,
      },
      computed: {
        name() {
          return this.isTom ? "Tom" : "Jerry";
        },
      },
      methods: {
        changeName() {
          this.isTom = !this.isTom;
        },
      },
      watch: {
        // 常规操作
        // name:{
        //   immediate:true, //初始化就执行handler方法
        //   // isTom发生改变时执行
        //   handler(newValue,oldValue){
        //     console.log("名字切换了", oldValue + "->" + newValue)
        //   }
        // },

        // 简写
        name(newValue, oldValue) {
          console.log("名字切换了", oldValue + "->" + newValue);
        },
      },
    });
    // 正常写法
    // vm.$watch('isTom',{
    //   immediate:true, //初始化就执行handler方法
    //   // isTom发生改变时执行
    //   handler(newValue,oldValue){
    //     console.log("isTom切换了", oldValue + "->" + newValue)
    //   }
    // })

    // 简写
    vm.$watch("isTom", function (newValue, oldValue) {
      console.log("isTom切换了", oldValue + "->" + newValue);
    });
  </script>
</html>
  • 效果
相关推荐
是梦终空2 小时前
计算机毕业设计252—基于Java+Springboot+vue3+协同过滤推荐算法的农产品销售系统(源代码+数据库+2万字论文)
java·spring boot·vue·毕业设计·源代码·协同过滤算法·农产品销售系统
Beginner x_u7 小时前
从组件点击事件到业务统一入口:一次前端操作链的完整解耦实践
前端·javascript·vue·业务封装
苏打水com11 小时前
第十篇:Day28-30 工程化优化与部署——从“能跑”到“好用”(对标职场“项目上线”需求)
前端·css·vue·html·js
武哥聊编程12 小时前
【从0带做】基于Springboot3+Vue3的生态养殖管理系统
java·学习·vue·毕业设计·springboot
xiaoxue..13 小时前
从 “手动搬砖“ 到 “自动施法“:界面开发的三次 “渡劫“ 升级记
前端·前端框架·vue
@AfeiyuO1 天前
封装ElementPlusIcons图标和系统应用内置图片为应用图标
vue
JELEE.1 天前
Vue3复习笔记
vue.js·笔记·vue
by__csdn2 天前
第二章 (.NET Core环境搭建)第二节( Visual Studio Code)
ide·vscode·c#·vue·asp.net·.net·.netcore
是梦终空2 天前
JAVA毕业设计253—基于Java+Springboot+vue3+协同过滤推荐算法的传统服饰文化平台(源代码+数据库+任务书+12000字论文)
java·spring boot·vue·毕业设计·课程设计·协同过滤推荐算法·传统服饰文化平台
水宁成冰2 天前
前端静态网站Lighthouse评分优化,vue3项目
前端·vue