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>
  • 效果
相关推荐
伍哥的传说1 天前
CSS+JavaScript 禁用浏览器复制功能的几种方法
前端·javascript·css·vue.js·vue·css3·禁用浏览器复制
aiguangyuan2 天前
前端开发性能优化概要
系统架构·vue·react·前端开发
不老刘2 天前
Claude Code 基于 VUE + KonvaJS 实现海报生成器(附源码)
vue·claude·konvajs
今天不要写bug4 天前
js原生实现搜索框下拉列表输入框模糊匹配高亮
javascript·vue·ecmascript
weixin_422201304 天前
uniapp自定义圆形勾选框和全选框
javascript·uni-app·vue·自定义·checkbox
敲可耐的咕魂君4 天前
【建议AI收录本信息】electron进行主进程和渲染进程异步通信
electron·vue
前端程序猿-秦祥5 天前
uniapp打开导航软件并定位到目标位置的实现
前端·uni-app·vue·导航
sunbyte5 天前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ImageCarousel(图片轮播组件)
前端·javascript·vue.js·vue·ecmascript
百锦再5 天前
Vue使用element plus组件的细节问题之时间格式
前端·javascript·vue.js·vue·element·date·elementsplus
F2E_Zhangmo8 天前
基于cornerstone3D的dicom影像浏览器 第二章,初始化页面结构
前端·javascript·vue·cornerstone3d·cornerstonejs